PetsFirebaseMessagingService.java 8.1 KB

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