Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. package me.pushapp.medime.firebase;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.graphics.Color;
  12. import android.media.RingtoneManager;
  13. import android.os.Build;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.util.Log;
  16.  
  17. import com.google.firebase.messaging.FirebaseMessagingService;
  18. import com.google.firebase.messaging.RemoteMessage;
  19.  
  20. import java.util.Map;
  21.  
  22. import me.pushapp.medime.R;
  23. import me.pushapp.medime.app.home.HomeActivity_;
  24.  
  25. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  26. private static final String TAG = "FirebaseServiceMM";
  27. public static final String dispenserID = "dispenserId";
  28.  
  29. @Override
  30. public void onMessageReceived(RemoteMessage remoteMessage) {
  31. super.onMessageReceived(remoteMessage);
  32. RemoteMessage.Notification notification = remoteMessage.getNotification();
  33. Map<String, String> data = remoteMessage.getData();
  34. String dispenserId = null;
  35. for ( String key : data.keySet() ) {
  36. Log.d(TAG, key);
  37. if(key.equals(dispenserID)){
  38. dispenserId = data.get(key);
  39. break;
  40. }
  41. }
  42. Log.d(TAG, "dispenserId: "+dispenserId);
  43. Log.d(TAG, "onMessageReceived: ::before");
  44. sendNotification(notification, data,dispenserId);
  45. }
  46.  
  47. private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data, String dispenserId) {
  48. Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifica);
  49.  
  50. Intent intent = new Intent(this, HomeActivity_.class);
  51. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  52. intent.putExtra(dispenserID,dispenserId);
  53. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  54.  
  55. Log.d(TAG, "onMessageReceived: ::before notificationBuilder");
  56. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
  57. .setContentTitle(notification.getTitle())
  58. .setContentText(notification.getBody())
  59. .setAutoCancel(true)
  60. .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  61. .setContentIntent(pendingIntent)
  62. .setContentInfo(notification.getTitle())
  63. .setLargeIcon(icon)
  64. .setColor(Color.parseColor("#4083B3"))
  65. .setLights(Color.parseColor("#4083B3"), 1000, 300)
  66. .setDefaults(Notification.DEFAULT_VIBRATE)
  67. .setSmallIcon(R.drawable.ic_notifica);
  68. Log.d(TAG, "onMessageReceived: ::after notificationBuilder");
  69.  
  70. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  71.  
  72. // Il NotificationChannel è richiesto da Android O in poi
  73. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  74. NotificationChannel channel = new NotificationChannel(
  75. getString(R.string.default_notification_channel_id),
  76. getString(R.string.default_notification_channel_name),
  77. NotificationManager.IMPORTANCE_DEFAULT
  78. );
  79. channel.setDescription("Notifiche di MediMe");
  80. channel.setShowBadge(true);
  81. channel.canShowBadge();
  82. channel.enableLights(true);
  83. channel.setLightColor(Color.parseColor("#4083B3"));
  84. channel.enableVibration(true);
  85. channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
  86. notificationManager.createNotificationChannel(channel);
  87. }
  88.  
  89. notificationManager.notify(0, notificationBuilder.build());
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement