No Description

MyFirebaseMessagingService.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package uprrp.tania.services;
  2. import com.google.firebase.messaging.FirebaseMessagingService;
  3. import com.google.firebase.messaging.RemoteMessage;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.app.TaskStackBuilder;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.media.RingtoneManager;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.util.Log;
  14. import androidx.core.app.NotificationCompat;
  15. import androidx.core.app.NotificationManagerCompat;
  16. import uprrp.tania.R;
  17. import uprrp.tania.activities.MainActivity;
  18. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  19. private static final String TAG = "MyFirebaseMsgService";
  20. /**
  21. * Called when message is received.
  22. *
  23. * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
  24. */
  25. @Override
  26. public void onMessageReceived(RemoteMessage remoteMessage) {
  27. // [START_EXCLUDE]
  28. // There are two types of messages data messages and notification messages. Data messages
  29. // are handled
  30. // here in onMessageReceived whether the app is in the foreground or background. Data
  31. // messages are the type
  32. // traditionally used with GCM. Notification messages are only received here in
  33. // onMessageReceived when the app
  34. // is in the foreground. When the app is in the background an automatically generated
  35. // notification is displayed.
  36. // When the user taps on the notification they are returned to the app. Messages
  37. // containing both notification
  38. // and data payloads are treated as notification messages. The Firebase console always
  39. // sends notification
  40. // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
  41. // [END_EXCLUDE]
  42. Log.d(TAG, "From: " + remoteMessage.getFrom());
  43. // Check if message contains a data payload.
  44. if (remoteMessage.getData().size() > 0) {
  45. Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  46. sendNotification(remoteMessage.getData().toString());
  47. if (/* Check if data needs to be processed by long running job */ true) {
  48. // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
  49. //scheduleJob();
  50. } else {
  51. // Handle message within 10 seconds
  52. handleNow();
  53. }
  54. }
  55. // Check if message contains a notification payload.
  56. if (remoteMessage.getNotification() != null) {
  57. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  58. Intent resultIntent = new Intent(this, MainActivity.class);
  59. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  60. stackBuilder.addNextIntentWithParentStack(resultIntent);
  61. PendingIntent resultPendingIntent =
  62. stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  63. NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id));
  64. builder.setContentIntent(resultPendingIntent);
  65. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
  66. notificationManager.notify(0, builder.build());
  67. // String channelId = getString(R.string.default_notification_channel_id);
  68. //
  69. // Intent intent = new Intent(this, MainActivity.class);
  70. // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  71. // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
  72. // PendingIntent.FLAG_ONE_SHOT);
  73. //
  74. //
  75. // Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  76. // NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  77. // .setSmallIcon(R.mipmap.ic_launcher)
  78. // .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
  79. // .setContentTitle("Title")
  80. // .setVibrate(new long[]{5000})
  81. //// .setLights(Color.GREEN, 3000, 3000) //for notification led light with color
  82. // .setContentText(remoteMessage.getNotification().getBody())
  83. // .setAutoCancel(true)
  84. // .setSound(soundUri)
  85. // .setContentIntent(pendingIntent);
  86. //
  87. // NotificationManager notificationManager =
  88. // (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  89. // notificationManager.notify(0, notificationBuilder.build());
  90. }
  91. // Also if you intend on generating your own notifications as a result of a received FCM
  92. // message, here is where that should be initiated. See sendNotification method below.
  93. }
  94. // [END receive_message]
  95. // [START on_new_token]
  96. /**
  97. * Called if InstanceID token is updated. This may occur if the security of
  98. * the previous token had been compromised. Note that this is called when the InstanceID token
  99. * is initially generated so this is where you would retrieve the token.
  100. */
  101. @Override
  102. public void onNewToken(String token) {
  103. Log.d(TAG, "Refreshed token: " + token);
  104. // If you want to send messages to this application instance or
  105. // manage this apps subscriptions on the server side, send the
  106. // Instance ID token to your app server.
  107. sendRegistrationToServer(token);
  108. }
  109. // [END on_new_token]
  110. /**
  111. * Handle time allotted to BroadcastReceivers.
  112. */
  113. private void handleNow() {
  114. Log.d(TAG, "Short lived task is done.");
  115. }
  116. /**
  117. * Persist token to third-party servers.
  118. *
  119. * Modify this method to associate the user's FCM InstanceID token with any server-side account
  120. * maintained by your application.
  121. *
  122. * @param token The new token.
  123. */
  124. private void sendRegistrationToServer(String token) {
  125. // TODO: Implement this method to send token to your app server.
  126. }
  127. /**
  128. * Create and show a simple notification containing the received FCM message.
  129. *
  130. * @param messageBody FCM message body received.
  131. */
  132. private void sendNotification(String messageBody) {
  133. // Create an Intent for the activity you want to start
  134. Intent resultIntent = new Intent(this, MainActivity.class);
  135. // Create the TaskStackBuilder and add the intent, which inflates the back stack
  136. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  137. stackBuilder.addNextIntentWithParentStack(resultIntent);
  138. // Get the PendingIntent containing the entire back stack
  139. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  140. //Intent intent = new Intent(this, MainActivity.class);
  141. //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  142. //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 0);
  143. String channelId = getString(R.string.default_notification_channel_id);
  144. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  145. NotificationCompat.Builder notificationBuilder =
  146. new NotificationCompat.Builder(this, channelId)
  147. .setSmallIcon(R.drawable.ic_stat_ic_notification)
  148. .setContentTitle("New Notification!")
  149. .setContentText(messageBody)
  150. .setAutoCancel(true)
  151. .setSound(defaultSoundUri)
  152. .setContentIntent(resultPendingIntent);
  153. NotificationManager notificationManager =
  154. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  155. // Since android Oreo notification channel is needed.
  156. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  157. NotificationChannel channel = new NotificationChannel(channelId,
  158. "Channel human readable title",
  159. NotificationManager.IMPORTANCE_DEFAULT);
  160. notificationManager.createNotificationChannel(channel);
  161. }
  162. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  163. }
  164. }