Guest User

Untitled

a guest
Feb 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. private static final String TAG = "MyFMService";
  2. public boolean isAppOpened = true;
  3. public static boolean notificationReceived = false;
  4. Bundle b;
  5.  
  6. @Override
  7. public void onMessageReceived(RemoteMessage remoteMessage) {
  8. Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
  9. Log.d(TAG, "FCM Notification Message: " +
  10. remoteMessage.getNotification());
  11. Log.d(TAG, "FCM Data Message: " + remoteMessage.getData());
  12. showNotification(remoteMessage.getData());
  13. }
  14.  
  15.  
  16. public void showNotification(Map<String,String> r) {
  17. try {
  18. Intent intent;
  19. try {
  20. SessionContext.createSessionFromCurrentSession();
  21. }catch (IllegalStateException e){
  22. isAppOpened=false;
  23. }
  24. if (isAppOpened) {
  25. notificationReceived=true;
  26. if (!(r.get("type").equals("0"))) {
  27. Notifications.notificationId = Long.parseLong(r.get("notificationId"));
  28. NewPost.postType = (SessionContext.getUserId() ==
  29. Long.parseLong(r.get("postedUserId"))) ?
  30. getResources().getString(R.string.post_type_my_post) :
  31. getResources().getString(R.string.post_type_relevant_post);
  32.  
  33. Map<String, Object> postMap = new HashMap<>();
  34.  
  35. postMap.put("threadId", r.get("threadId"));
  36. postMap.put("profileUrl", r.get("profileUrl"));
  37. postMap.put("firstName", r.get("firstName"));
  38. postMap.put("lastName", r.get("lastName"));
  39. postMap.put("categoryId", r.get("categoryId"));
  40. postMap.put("timestamp", Long.parseLong(r.get("timeStamp")));
  41. postMap.put("subject", r.get("subject"));
  42. postMap.put("threadStatus", r.get("threadStatus"));
  43. postMap.put("userId", String.valueOf(SessionContext.getUserId()));
  44. postMap.put("postedUserId", r.get("postedUserId"));
  45. postMap.put("userName", r.get("userName"));
  46.  
  47. ViewThread.post = new Post(postMap);
  48. NewPost.currentPostId = ViewThread.post.getThreadId();
  49. NewPost.currentPosition = (SessionContext.getUserId() ==
  50. Long.parseLong(r.get("postedUserId"))) ? 0 : 1;
  51. NewPost.postedUserId = Long.parseLong(ViewThread.post.getPostedUserId());
  52. HomeScreen.categoryId = Long.parseLong(ViewThread.post.getCategoryId());
  53.  
  54. intent = new Intent(this, ViewThread.class);
  55. intent.putExtra("THREAD", ViewThread.post);
  56. // startActivity(intent);
  57.  
  58. } else {
  59. intent = new Intent(this, Notifications.class);
  60. }
  61. } else {
  62. notificationReceived=true;
  63. intent = new Intent(this, SplashScreen.class);
  64. }
  65.  
  66.  
  67.  
  68.  
  69. PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
  70. NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  71. .setContentTitle("New Notification")
  72. .setContentText(r.get("notification"))
  73. .setSmallIcon(R.drawable.d4e_logo)
  74. .setContentIntent(pIntent)
  75. .setAutoCancel(true)
  76. .addAction(R.drawable.view_icon, "View", pIntent);
  77. /* .addAction(R.drawable.icon, "More", pIntent)
  78. .addAction(R.drawable.icon, "And more", pIntent).build();*/
  79.  
  80.  
  81. NotificationManager notificationManager =
  82. (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  83.  
  84. notificationManager.notify(0, builder.build());
  85. } catch (Exception exception) {
  86. exception.printStackTrace();
  87. }
  88. }
Add Comment
Please, Sign In to add comment