Advertisement
rodrigofbm

NotificationService

Apr 28th, 2020
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package br.com.vannon.drogariacatarinense;
  2.  
  3. import android.content.Intent;
  4. import android.content.Context;
  5. import android.support.v4.content.LocalBroadcastManager;
  6. import android.support.v4.app.NotificationCompat;
  7. import android.support.v4.app.NotificationManagerCompat;
  8. import android.app.PendingIntent;
  9. import android.app.Notification;
  10. import android.app.NotificationManager;
  11. import android.app.NotificationChannel;
  12. import android.graphics.Color;
  13. import android.media.RingtoneManager;
  14. import android.net.Uri;
  15. import android.os.Build;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import io.invertase.firebase.R;
  19.  
  20. import com.facebook.react.HeadlessJsTaskService;
  21. import com.google.firebase.messaging.FirebaseMessagingService;
  22. import com.google.firebase.messaging.RemoteMessage;
  23.  
  24. import io.invertase.firebase.Utils;
  25.  
  26. import java.util.Map;
  27. import java.util.HashMap;
  28. import java.util.Iterator;
  29.  
  30. import org.json.JSONObject;
  31. import org.json.JSONException;
  32.  
  33. import br.com.vannon.drogariacatarinense.MainActivity;
  34.  
  35. public class NotificationService extends FirebaseMessagingService {
  36.  
  37.   private static final String TAG = "NotificationService";
  38.   public static final String REMOTE_NOTIFICATION_EVENT = "notifications-remote-notification";
  39.  
  40.   @Override
  41.   public void onMessageReceived(RemoteMessage message) {
  42.     Log.d(TAG, "onMessageReceived event received");
  43.     try {
  44.       Intent intent = new Intent(this, MainActivity.class);
  45.       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  46.  
  47.       PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  48.  
  49.       Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  50.       NotificationCompat.Builder notificationBuilder;
  51.       NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  52.  
  53.       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  54.  
  55.         notificationBuilder = new NotificationCompat.Builder(this, message.getData().get("android_channel_id"))
  56.                 .setSmallIcon(R.mipmap.ic_launcher)
  57.                 .setContentTitle(message.getData().get("title"))
  58.                 .setContentText(message.getData().get("body"))
  59.                 .setAutoCancel(true)
  60.                 .setSound(defaultSoundUri)
  61.                 .setPriority(NotificationCompat.PRIORITY_MAX)
  62.                 .setContentIntent(pendingIntent)
  63.                 .setContentText(message.getData().get("body"));
  64.       } else {
  65.         notificationBuilder = new NotificationCompat.Builder(this)
  66.                 .setSmallIcon(R.mipmap.ic_launcher)
  67.                 .setContentTitle(message.getData().get("title"))
  68.                 .setContentText(message.getData().get("body"))
  69.                 .setAutoCancel(true)
  70.                 .setSound(defaultSoundUri)
  71.                 .setPriority(NotificationCompat.PRIORITY_MAX)
  72.                 .setContentIntent(pendingIntent)
  73.                 .setContentText(message.getData().get("body"));
  74.       }
  75.  
  76.       notificationManager.notify(0, notificationBuilder.build());
  77.     } catch (Exception e) {
  78.       Log.d(TAG, "Error ", e);
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement