Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  2.  
  3. private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
  4.  
  5. private NotificationUtil notificationUtil;
  6.  
  7. @Override
  8. public void onMessageReceived(RemoteMessage remoteMessage) {
  9. Log.e(TAG, "From: " + remoteMessage.getFrom());
  10.  
  11. if (remoteMessage == null)
  12. return;
  13.  
  14. // Check if message contains a notification payload.
  15. if (remoteMessage.getNotification() != null) {
  16. Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
  17. handleNotification(remoteMessage.getNotification().getBody());
  18. }
  19.  
  20. // Check if message contains a data payload.
  21. if (remoteMessage.getData().size() > 0) {
  22. Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
  23.  
  24. try {
  25. JSONObject json = new JSONObject(remoteMessage.getData().toString());
  26. handleDataMessage(json);
  27. } catch (Exception e) {
  28. Log.e(TAG, "Exception: " + e.getMessage());
  29. }
  30. }
  31. }
  32.  
  33. private void handleNotification(String message) {
  34. if (!NotificationUtil.isAppIsInBackground(getApplicationContext())) {
  35. // app is in foreground, broadcast the push message
  36. Intent pushNotification = new Intent(NotificationConfig.PUSH_NOTIFICATION);
  37. pushNotification.putExtra("message", message);
  38. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  39.  
  40. // play notification sound
  41. NotificationUtil notificationUtils = new NotificationUtil(getApplicationContext());
  42. notificationUtils.playNotificationSound();
  43. }else{
  44. // If the app is in background, firebase itself handles the notification
  45. }
  46. }
  47.  
  48. private void handleDataMessage(JSONObject json) {
  49. Log.e(TAG, "push json: " + json.toString());
  50.  
  51. try {
  52. JSONObject data = json.getJSONObject("data");
  53.  
  54. String title = data.getString("title");
  55. String message = data.getString("message");
  56. boolean isBackground = data.getBoolean("is_background");
  57. String imageUrl = data.getString("image");
  58. String timestamp = data.getString("timestamp");
  59. JSONObject payload = data.getJSONObject("payload");
  60.  
  61. Log.e(TAG, "title: " + title);
  62. Log.e(TAG, "message: " + message);
  63. Log.e(TAG, "isBackground: " + isBackground);
  64. Log.e(TAG, "payload: " + payload.toString());
  65. Log.e(TAG, "imageUrl: " + imageUrl);
  66. Log.e(TAG, "timestamp: " + timestamp);
  67.  
  68.  
  69. if (!NotificationUtil.isAppIsInBackground(getApplicationContext())) {
  70. // app is in foreground, broadcast the push message
  71. Intent pushNotification = new Intent(NotificationConfig.PUSH_NOTIFICATION);
  72. pushNotification.putExtra("message", message);
  73. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  74.  
  75. // play notification sound
  76. NotificationUtil notificationUtil = new NotificationUtil(getApplicationContext());
  77. notificationUtil.playNotificationSound();
  78. } else {
  79. // app is in background, show the notification in notification tray
  80. Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
  81. resultIntent.putExtra("message", message);
  82.  
  83. // check for image attachment
  84. if (TextUtils.isEmpty(imageUrl)) {
  85. showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
  86. } else {
  87. // image is present, show notification with image
  88. showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
  89. }
  90. }
  91. } catch (JSONException e) {
  92. Log.e(TAG, "JSON Exception: " + e.getMessage());
  93. } catch (Exception e) {
  94. Log.e(TAG, "Exception: " + e.getMessage());
  95. }
  96. }
  97.  
  98. /**
  99. * Showing notification with text only
  100. */
  101. private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
  102. notificationUtil = new NotificationUtil(context);
  103. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  104. notificationUtil.showNotificationMessage(title, message, timeStamp, intent);
  105. }
  106.  
  107. /**
  108. * Showing notification with text and image
  109. */
  110. private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
  111. notificationUtil = new NotificationUtil(context);
  112. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  113. notificationUtil.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
  114. }
  115.  
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement