tegarkurniawan

notification

Jul 20th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. public static final int ID_BIG_NOTIFICATION = 234;
  2. public static final int ID_SMALL_NOTIFICATION = 235;
  3.  
  4. private Context mCtx;
  5.  
  6. public MyNotificationManager(Context mCtx) {
  7. this.mCtx = mCtx;
  8. }
  9.  
  10. //the method will show a big notification with an image
  11. //parameters are title for message title, message for message text, url of the big image and an intent that will open
  12. //when you will tap on the notification
  13. public void showBigNotification(String title, String message, String url, Intent intent) {
  14. PendingIntent resultPendingIntent =
  15. PendingIntent.getActivity(
  16. mCtx,
  17. ID_BIG_NOTIFICATION,
  18. intent,
  19. PendingIntent.FLAG_UPDATE_CURRENT
  20. );
  21. Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  22. NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
  23. bigPictureStyle.setBigContentTitle(title);
  24. bigPictureStyle.setSummaryText(Html.fromHtml(message));
  25. bigPictureStyle.bigPicture(getBitmapFromURL(url));
  26. // try {
  27. // bigPictureStyle.bigPicture(Picasso.with(mCtx).load(url).networkPolicy(NetworkPolicy.NO_CACHE).memoryPolicy(MemoryPolicy.NO_CACHE).skipMemoryCache().get());
  28. // } catch (IOException e) {
  29. // e.printStackTrace();
  30. // }
  31. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
  32. Notification notification;
  33. Intent notificationIntent = new Intent(mCtx, NotificationActivity.class);
  34.  
  35. notificationIntent.putExtra("title",title);
  36. notificationIntent.putExtra("message", message);
  37. notificationIntent.putExtra("image", url);
  38.  
  39. // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
  40. // Intent.FLAG_ACTIVITY_SINGLE_TOP);
  41. notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  42. notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  43. PendingIntent contentIntent = PendingIntent.getActivity(mCtx,
  44. new Random().nextInt(), notificationIntent,
  45. PendingIntent.FLAG_UPDATE_CURRENT);
  46. notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
  47. .setAutoCancel(true)
  48. .setContentIntent(resultPendingIntent)
  49. .setContentTitle(title)
  50. .setStyle(bigPictureStyle)
  51. .setPriority(Notification.PRIORITY_MAX)
  52. .setSmallIcon(R.mipmap.ic_launcher)
  53. .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
  54. .setContentText(message)
  55. .setSound(alarmSound)
  56. .setContentIntent(contentIntent).setAutoCancel(true)
  57. .build();
  58.  
  59.  
  60. //notification.flags |= Notification.FLAG_AUTO_CANCEL;
  61.  
  62. NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
  63. notificationManager.notify(ID_BIG_NOTIFICATION, notification);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment