Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  2.  
  3. private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
  4.  
  5.  
  6. @Override
  7. public void onMessageReceived(RemoteMessage remoteMessage) {
  8. Log.e(TAG, "From: " + remoteMessage.getFrom());
  9.  
  10. if (remoteMessage == null)
  11. return;
  12.  
  13. // Check if message contains a notification payload.
  14. if (remoteMessage.getNotification() != null) {
  15. handleNotification(remoteMessage.getNotification().getBody());
  16. showSimpleMsg(remoteMessage.getNotification());
  17. }
  18.  
  19. // Check if message contains a data payload.
  20. if (remoteMessage.getData().size() > 0) {
  21. try {
  22. JSONObject json = new JSONObject(remoteMessage.getData());
  23. storeNotificationToPreferences(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),
  24. json);
  25.  
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31.  
  32. private void showSimpleMsg(RemoteMessage.Notification notification) {
  33. Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  34. pushNotification.putExtra("message", notification.getBody());
  35. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  36.  
  37. showNotificationMessage(getApplicationContext(), notification.getTitle(), notification.getBody());
  38. }
  39.  
  40. private void handleNotification(String message) {
  41. if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
  42. // app is in foreground, broadcast the push message
  43. Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  44. pushNotification.putExtra("message", message);
  45. LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  46.  
  47. // play notification sound
  48. NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
  49. notificationUtils.playNotificationSound();
  50. } else {
  51. // If the app is in background, firebase itself handles the notification
  52. }
  53. }
  54.  
  55.  
  56. private void showNotificationMessage(Context context, String title, String message) {
  57.  
  58.  
  59. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  60. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  61. String CHANNEL_ID = "olonce";
  62. NotificationChannel notificationChannel = null;
  63. notificationChannel = new NotificationChannel(CHANNEL_ID, "olonce", NotificationManager.IMPORTANCE_LOW);
  64. PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
  65. Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
  66. .setContentText(message)
  67. .setContentTitle(title)
  68. .setContentIntent(pendingIntent)
  69. .setChannelId(CHANNEL_ID)
  70. .setSmallIcon(R.mipmap.ic_launcher)
  71.  
  72. .build();
  73.  
  74. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  75. notificationManager.createNotificationChannel(notificationChannel);
  76. notificationManager.notify(1, notification);
  77. } else {
  78. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  79.  
  80. PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
  81. Notification notification = new Notification.Builder(getApplicationContext())
  82. .setContentText(message)
  83. .setContentTitle(title)
  84. .setContentIntent(pendingIntent)
  85. .setSmallIcon(R.mipmap.ic_launcher)
  86.  
  87. .build();
  88.  
  89. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  90. notificationManager.notify(1, notification);
  91. }
  92. }
  93.  
  94.  
  95. private void storeNotificationToPreferences(String title, String message,JSONObject extraData) {
  96. SharedPreferences preferences = getApplicationContext().getSharedPreferences("Notifications", MODE_PRIVATE);
  97.  
  98. try {
  99. //Default Data
  100. JSONObject jsonObject = new JSONObject();
  101. jsonObject.put("data", new JSONArray());
  102. jsonObject.put("unread", 0);
  103.  
  104. JSONObject oldData = new JSONObject(preferences.getString("notifications", jsonObject.toString()));
  105. JSONArray data = oldData.getJSONArray("data");
  106. data.put(new JSONObject().put("title", title).put("message", message).put("data",extraData));
  107.  
  108. int unread = (oldData.has("unread") ? oldData.getInt("unread") : 0) + 1;
  109. oldData.put("unread", unread);
  110.  
  111. SharedPreferences.Editor editor = preferences.edit();
  112. editor.remove("notifications");
  113. editor.putString("notifications", oldData.toString());
  114. editor.commit();
  115.  
  116. EventBus.getDefault().post(Common.EVENTS.NOTIFICATION);
  117. } catch (JSONException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement