Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. package premiumcard.app.services;
  2.  
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.media.RingtoneManager;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.util.Log;
  13.  
  14. import androidx.core.app.NotificationCompat;
  15.  
  16. import com.google.firebase.messaging.FirebaseMessagingService;
  17. import com.google.firebase.messaging.RemoteMessage;
  18.  
  19. import premiumcard.app.BaseApplication;
  20. import premiumcard.app.R;
  21. import timber.log.Timber;
  22.  
  23. // FCM PushListener IMPLEMENTATION
  24.  
  25. /*
  26. * This service is designed to run in the background and receive messages from gcm. If the app is in the foreground
  27. * when a message is received, it will immediately be posted. If the app is not in the foreground, the message will be saved
  28. * and a notification is posted to the NotificationManager.
  29. */
  30.  
  31.  
  32. public class MyFirebaseMessagingService extends FirebaseMessagingService
  33. {
  34.  
  35. public static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
  36.  
  37.  
  38. /**
  39. * This method is called if InstanceID token is updated. This may occur if the security of
  40. * the previous token had been compromised. Note that this is called when the InstanceID token
  41. * is initially generated so this is where you would retrieve the token.
  42. */
  43.  
  44. public void onNewToken(String token) {
  45. try {
  46. super.onNewToken(token);
  47. Log.d(TAG, "FCM_Token: " + token);
  48.  
  49. // TODO: Implement this method to send any registration to your app's servers.
  50. sendRegistrationToServer(token);
  51. } catch (Exception e) {
  52. Log.d(TAG, "Failed to complete token refresh", e);
  53. }
  54. }
  55.  
  56. /**
  57. * Persist token to third-party servers.
  58. *
  59. * Modify this method to associate the user's FCM InstanceID token with any server-side account
  60. * maintained by your application.
  61. *
  62. * @param token The new token.
  63. */
  64.  
  65. private void sendRegistrationToServer(String token) {
  66. Timber.i("FB Token = " + token);
  67. // TODO: send the token to SNS and create platform endpoint
  68. }
  69.  
  70.  
  71. @Override
  72.  
  73. public void onMessageReceived(RemoteMessage remoteMessage) {
  74.  
  75. // [START_EXCLUDE]
  76. /*There are two types of messages : data messages and notification messages.
  77.  
  78. Data messages are handled here in onMessageReceived whether the app is in the foreground or background.
  79.  
  80. Notification messages are only received here in onMessageReceived when the app is in the foreground.
  81. When the app is in the background, an automatically generated notification is displayed.
  82.  
  83. Messages containing both notification and data payloads are treated as notification messages.
  84.  
  85. The Firebase console always sends notification messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options*
  86. */
  87. // [STOP_EXCLUDE]
  88.  
  89. // TODO(developer): Handle FCM messages here.
  90. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
  91.  
  92. Log.d(TAG, "From: " + remoteMessage.getFrom());
  93.  
  94. // Check if message contains a data payload.
  95. Intent intent = new Intent(this, BaseApplication.getmInstance().activeActivity.getClass());
  96.  
  97. if (remoteMessage.getData().size() > 0) {
  98. Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  99.  
  100. }
  101.  
  102. // Check if message contains a notification payload.
  103. if (remoteMessage.getNotification() != null) {
  104. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  105. String messageBody = remoteMessage.getData().get("message");
  106. displayNotificationMessage(messageBody);
  107. }
  108. }
  109.  
  110. /**
  111. * Create and show a simple notification containing the received FCM Notification message.
  112. *
  113. * @param messageBody FCM message body received.
  114. */
  115.  
  116. public void displayNotificationMessage(String messageBody) {
  117. Intent intent = new Intent(this, BaseApplication.getmInstance().activeActivity.getClass());
  118. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  119. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  120. PendingIntent.FLAG_CANCEL_CURRENT);
  121.  
  122. String channelId = getString(R.string.default_notification_channel_id);
  123. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  124. NotificationCompat.Builder notificationBuilder =
  125. new NotificationCompat.Builder(this, channelId)
  126. .setContentTitle("TEst")
  127. .setContentText(messageBody)
  128. .setContentIntent(pendingIntent)
  129. .setAutoCancel(true)
  130. .setSound(defaultSoundUri)
  131. .setContentIntent(pendingIntent)
  132. .setColor(Color.parseColor("#3F5996"));
  133.  
  134. NotificationManager notificationManager =
  135. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  136.  
  137. // Since android Oreo notification channel is needed.
  138. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  139. NotificationChannel channel = new NotificationChannel(channelId,
  140. "Channel human readable title",
  141. NotificationManager.IMPORTANCE_DEFAULT);
  142. notificationManager.createNotificationChannel(channel);
  143. }
  144.  
  145. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement