Guest User

Untitled

a guest
Jan 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. private void sendNotification(String title, String body, String url, String badge) {
  2. Intent intent = new Intent(this, MainActivity.class);
  3. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  4.  
  5. if (Patterns.WEB_URL.matcher(url).matches()) {
  6. intent.putExtra("link", url);
  7. }
  8.  
  9. PendingIntent pendingIntent = PendingIntent.getActivity(
  10. this,
  11. 0,
  12. intent,
  13. PendingIntent.FLAG_UPDATE_CURRENT
  14. );
  15.  
  16. Resources resources = getApplicationContext().getResources();
  17.  
  18. NotificationCompat.Builder notificationBuilder =
  19. new NotificationCompat.Builder(this, "default")
  20. .setColor(
  21. resources.getColor(R.color.colorPrimaryDark)
  22. )
  23. .setSmallIcon(
  24. R.drawable.ic_stat_icon
  25. )
  26. .setContentTitle(title)
  27. .setContentText(body)
  28. .setAutoCancel(true)
  29. .setNumber(Integer.parseInt(badge))
  30. .setLargeIcon(
  31. BitmapFactory.decodeResource(
  32. resources,
  33. R.mipmap.ic_launcher
  34. )
  35. )
  36. .setContentIntent(pendingIntent);
  37.  
  38. NotificationManager notificationManager =
  39. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  40.  
  41. if (Build.VERSION.SDK_INT >= 26) {
  42. NotificationChannel notificationChannel = new NotificationChannel(
  43. "default",
  44. "Main notification channel",
  45. NotificationManager.IMPORTANCE_HIGH
  46. );
  47.  
  48. notificationManager.createNotificationChannel(
  49. notificationChannel
  50. );
  51. }
  52.  
  53. notificationManager.notify(
  54. 1,
  55. notificationBuilder.build()
  56. );
  57. }
  58.  
  59. <meta-data
  60. android:name="com.google.firebase.messaging.default_notification_icon"
  61. android:resource="@drawable/ic_stat_icon" />
  62. <meta-data
  63. android:name="com.google.firebase.messaging.default_notification_color"
  64. android:resource="@color/colorPrimaryDark" />
Add Comment
Please, Sign In to add comment