Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. public class MyNotificationManager {
  2.  
  3. public static final int ID_BIG_NOTIFICATION = 234;
  4. public static final int ID_SMALL_NOTIFICATION = 235;
  5.  
  6. private Context mCtx;
  7.  
  8. public MyNotificationManager(Context mCtx) {
  9. this.mCtx = mCtx;
  10. }
  11.  
  12. //the method will show a big notification with an image
  13. //parameters are title for message title, message for message text, url of the big image and an intent that will open
  14. //when you will tap on the notification
  15. public void showBigNotification(String title, String message, Bitmap image, Intent intent) {
  16. int requestID = (int) System.currentTimeMillis();
  17. PendingIntent resultPendingIntent =
  18. PendingIntent.getActivity(
  19. mCtx,
  20. requestID,
  21. intent,
  22. 0
  23. );
  24. Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
  25. bigPictureStyle.bigPicture(image);
  26. bigPictureStyle.setSummaryText(message);
  27. Notification.Builder mBuilder = new Notification.Builder(mCtx);
  28. Notification notification;
  29. notification = mBuilder.setSmallIcon(R.drawable.notification_icon).setTicker(title).setWhen(0)
  30. .setAutoCancel(true)
  31. .setContentTitle(title)
  32. .setStyle(bigPictureStyle)
  33. .setSmallIcon(R.drawable.notification_icon)
  34. .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.mr_hot_circle))
  35. .addAction(R.drawable.logout_icon,"Open App",resultPendingIntent)
  36. //.setContentIntent(resultPendingIntent)
  37. .build();
  38.  
  39.  
  40. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  41.  
  42. NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
  43. notificationManager.notify(requestID, notification);
  44. }
  45.  
  46. //the method will show a small notification
  47. //parameters are title for message title, message for message text and an intent that will open
  48. //when you will tap on the notification
  49. public void showSmallNotification(String title, String message, Intent intent) {
  50. int requestID = (int) System.currentTimeMillis();
  51. PendingIntent resultPendingIntent =
  52. PendingIntent.getActivity(
  53. mCtx,
  54. requestID,
  55. intent,
  56. 0
  57. );
  58.  
  59.  
  60. Notification.Builder mBuilder = new Notification.Builder(mCtx);
  61. Notification notification;
  62. notification = mBuilder.setSmallIcon(R.drawable.notification_icon).setTicker(title).setWhen(0)
  63. .setAutoCancel(true)
  64. .setContentIntent(resultPendingIntent)
  65. .setContentTitle(title)
  66. .setSmallIcon(R.drawable.notification_icon)
  67. .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.mr_hot_circle))
  68. .setContentText(message)
  69. .build();
  70.  
  71. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  72.  
  73. NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
  74. notificationManager.notify(requestID, notification);
  75. }
  76.  
  77. //The method will return Bitmap from an image URL
  78.  
  79. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  80. private static final String TAG = "MyFirebaseMsgService";
  81. MyNotificationManager mNotificationManager;
  82. String title,message,imageUrl;
  83. Intent intent;
  84. @Override
  85. public void onMessageReceived(RemoteMessage remoteMessage) {
  86. if (remoteMessage.getData().size() > 0) {
  87. Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
  88. try {
  89.  
  90. if (remoteMessage.getNotification() != null) {
  91. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  92. }
  93. title = remoteMessage.getData().get("title");
  94. message = remoteMessage.getData().get("message");
  95. imageUrl = remoteMessage.getData().get("image");
  96.  
  97. //creating MyNotificationManager object
  98. mNotificationManager = new MyNotificationManager(getApplicationContext());
  99.  
  100. //creating an intent for the notification
  101. intent = new Intent(getApplicationContext(), HomeActivity.class);
  102. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
  103. /*TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  104. stackBuilder.addParentStack(Welcome.class);
  105. stackBuilder.addNextIntent(intent);*/
  106.  
  107. //if there is no image
  108. if(imageUrl.equals("")){
  109. //displaying small notification
  110. mNotificationManager.showSmallNotification(title, message, intent);
  111. }else{
  112. //if there is an image
  113. //displaying a big notification
  114. getBitmapFromURL(imageUrl);
  115.  
  116. }
  117. } catch (Exception e) {
  118. Log.e(TAG, "Exception: " + e.getMessage());
  119. }
  120. }
  121. }
  122.  
  123.  
  124.  
  125. private void getBitmapFromURL(String strURL) {
  126. try {
  127. URL url = new URL(strURL);
  128. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  129. connection.setDoInput(true);
  130. connection.connect();
  131. InputStream input = connection.getInputStream();
  132. Bitmap myBitmap = BitmapFactory.decodeStream(input);
  133. mNotificationManager.showBigNotification(title, message, myBitmap, intent);
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136.  
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement