Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. @RequiresApi(api = Build.VERSION_CODES.O)
  2. @Override
  3. public void onMessageReceived(RemoteMessage remoteMessage) {
  4.  
  5. String identifyDataType = remoteMessage.getData().get(getString(R.string.data_type));
  6. //SITUATION: Application is in foreground then only send priority notificaitons such as an admin notification
  7. Log.d(TAG, "Data type: data: " +identifyDataType);
  8.  
  9. if(identifyDataType.equals(getString(R.string.data_type_admin_broadcast))){
  10. //build admin broadcast notification
  11. String title = remoteMessage.getData().get(getString(R.string.data_title));
  12. String senderId=remoteMessage.getData().get(getString(R.string.sender_of_message));
  13. String message = remoteMessage.getData().get(getString(R.string.data_message));
  14. Log.d(TAG,"TITL AND MESSG"+title+message);
  15. sendBroadcastNotification(title, message,senderId);
  16. }
  17. }
  18.  
  19.  
  20.  
  21. @RequiresApi(api = Build.VERSION_CODES.O)
  22. private void sendBroadcastNotification(String title, String message, String senderId){
  23. Log.d(TAG, "sendBroadcastNotification: building a admin broadcast notification with "+title+" "+message);
  24.  
  25. NotificationChannel mChannel=null;
  26. // Instantiate a Builder object.
  27. NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default_channel_id");
  28. // Creates an Intent for the Activity
  29. Intent notifyIntent = new Intent(this, MainActivity.class);
  30. notifyIntent.putExtra("Id", "none");
  31. Log.d("messsagingService Title",title);
  32. notifyIntent.putExtra("Name", title.split(":")[1].trim());
  33. // Sets the Activity to start in a new, empty task
  34. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  35. // Creates the PendingIntent
  36. PendingIntent notifyPendingIntent =
  37. PendingIntent.getActivity(
  38. this,
  39. 0,
  40. notifyIntent,
  41. PendingIntent.FLAG_UPDATE_CURRENT
  42. );
  43.  
  44. NotificationManager mNotificationManager =
  45. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  46.  
  47. mChannel = new NotificationChannel("default_channel_id", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
  48. // Configure the notification channel.
  49. mChannel.setDescription(message);
  50. mChannel.enableLights(true);
  51. mChannel.setLightColor(Color.RED);
  52. mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  53. mNotificationManager.createNotificationChannel(mChannel);
  54.  
  55. //add properties to the builder
  56. builder.setSmallIcon(R.drawable.icons1)
  57. .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
  58. R.drawable.icons1))
  59. .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  60. .setContentTitle(title)
  61. .setContentText(message)
  62. .setColor(getResources().getColor(R.color.blue_btn_bg_color))
  63. .setAutoCancel(true)
  64. .setStyle(new NotificationCompat.BigTextStyle()
  65. .bigText(title).setSummaryText(message))
  66. .setNumber(mNumPendingMessages)
  67. .setOnlyAlertOnce(true);
  68.  
  69. builder.setContentIntent(notifyPendingIntent);
  70.  
  71.  
  72.  
  73. mNotificationManager.notify(BROADCAST_NOTIFICATION_ID, builder.build());
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement