Advertisement
vergepuppeter

FirebaseMessaging

May 4th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. public class JGFirebaseMessagingService extends FirebaseMessagingService {
  2.  
  3.     private static final String TAG = "JGFirebaseMsgService";
  4.  
  5.     /**
  6.      * Called when message is received.
  7.      *
  8.      * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
  9.      */
  10.     // [START receive_message]
  11.     @Override
  12.     public void onMessageReceived(RemoteMessage remoteMessage) {
  13.         // [START_EXCLUDE]
  14.         // There are two types of messages data messages and notification messages. Data messages are handled
  15.         // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
  16.         // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
  17.         // is in the foreground. When the app is in the background an automatically generated notification is displayed.
  18.         // When the user taps on the notification they are returned to the app. Messages containing both notification
  19.         // and data payloads are treated as notification messages. The Firebase console always sends notification
  20.         // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
  21.         // [END_EXCLUDE]
  22.  
  23.         // TODO(developer): Handle FCM messages here.
  24.         // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
  25.         Log.d(TAG, "From: " + remoteMessage.getFrom());
  26.  
  27.         // Check if message contains a data payload.
  28.         if (remoteMessage.getData().size() > 0) {
  29.             Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  30.             try {
  31.                 JSONObject obj = new JSONObject(remoteMessage.getData());
  32.                 JSONObject gcmObject = obj.getJSONObject("GCM");
  33.                 JSONObject dataObject = gcmObject.getJSONObject("data");
  34.  
  35.                 sendNotification(dataObject.getString("title"), dataObject.getString("body"));
  36.             } catch (JSONException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.  
  41.         // Check if message contains a notification payload.
  42.         if (remoteMessage.getNotification() != null) {
  43.             Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  44.         }
  45.  
  46.         // Also if you intend on generating your own notifications as a result of a received FCM
  47.         // message, here is where that should be initiated. See sendNotification method below.
  48.     }
  49.     // [END receive_message]
  50.  
  51.  
  52.     private void sendNotification(String title, String message) {
  53.         Intent intent = new Intent(this, StoreLandingActivity.class);
  54.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  55.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  56.                 PendingIntent.FLAG_ONE_SHOT);
  57.  
  58.         Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  59.         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  60.                 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
  61.                 .setSmallIcon(R.mipmap.ic_launcher)
  62.                 .setContentTitle(title)
  63.                 .setContentText(message)
  64.                 .setAutoCancel(true)
  65.                 .setSound(defaultSoundUri)
  66.                 .setContentIntent(pendingIntent);
  67.  
  68.         NotificationManager notificationManager =
  69.                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  70.  
  71.         int defaults = 0;
  72.         defaults |= android.app.Notification.DEFAULT_SOUND;
  73.         defaults |= android.app.Notification.DEFAULT_VIBRATE;
  74.         notificationBuilder.setDefaults(defaults);
  75.  
  76.         notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement