|
@@ -0,0 +1,201 @@
|
|
|
+package com.vpn.fastestvpnservice.fcm;
|
|
|
+
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.media.RingtoneManager;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import androidx.core.app.NotificationCompat;
|
|
|
+import com.google.firebase.messaging.FirebaseMessagingService;
|
|
|
+import com.google.firebase.messaging.RemoteMessage;
|
|
|
+import com.vpn.fastestvpnservice.MainActivity;
|
|
|
+import com.vpn.fastestvpnservice.R;
|
|
|
+import com.vpn.fastestvpnservice.application.App;
|
|
|
+import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper;
|
|
|
+
|
|
|
+import kotlinx.coroutines.Job;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Created by
|
|
|
+ * Mykhailo on 11/16/2016.
|
|
|
+ */
|
|
|
+
|
|
|
+public class PetsFirebaseMessagingService extends FirebaseMessagingService {
|
|
|
+
|
|
|
+ private static final String TAG = "MyFirebaseMsgService";
|
|
|
+ BasePreferenceHelper prefHelper;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Called when message is received.
|
|
|
+ *
|
|
|
+ * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
|
|
|
+ */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessageReceived(RemoteMessage remoteMessage) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Log.d(TAG, "From: " + remoteMessage.getFrom());
|
|
|
+
|
|
|
+
|
|
|
+ if (remoteMessage.getData().size() > 0) {
|
|
|
+ Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
+ sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
|
|
|
+
|
|
|
+ if ( false) {
|
|
|
+
|
|
|
+ scheduleJob();
|
|
|
+ } else {
|
|
|
+
|
|
|
+ handleNow();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (remoteMessage.getNotification() != null) {
|
|
|
+ Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
|
|
|
+ sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Called if InstanceID token is updated. This may occur if the security of
|
|
|
+ * the previous token had been compromised. Note that this is called when the InstanceID token
|
|
|
+ * is initially generated so this is where you would retrieve the token.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onNewToken(String token) {
|
|
|
+ Log.d(TAG, "Refreshed token: " + token);
|
|
|
+
|
|
|
+ prefHelper = new BasePreferenceHelper(App.getApplication());
|
|
|
+ prefHelper.saveFcmToken(token);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ sendRegistrationToServer(token);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Schedule a job using FirebaseJobDispatcher.
|
|
|
+ */
|
|
|
+ private void scheduleJob() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Handle time allotted to BroadcastReceivers.
|
|
|
+ */
|
|
|
+ private void handleNow() {
|
|
|
+ Log.d(TAG, "Short lived task is done.");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Persist token to third-party servers.
|
|
|
+ * <p>
|
|
|
+ * Modify this method to associate the user's FCM InstanceID token with any server-side account
|
|
|
+ * maintained by your application.
|
|
|
+ *
|
|
|
+ * @param token The new token.
|
|
|
+ */
|
|
|
+ private void sendRegistrationToServer(String token) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Create and show a simple notification containing the received FCM message.
|
|
|
+ *
|
|
|
+ * @param title
|
|
|
+ * @param messageBody FCM message body received.
|
|
|
+ */
|
|
|
+ private void sendNotification(String title, String messageBody) {
|
|
|
+ Intent intent = new Intent(this, MainActivity.class);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ PendingIntent pendingIntent = null;
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ pendingIntent = PendingIntent.getActivity(this, 0 , intent,
|
|
|
+ PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ else{
|
|
|
+ pendingIntent = PendingIntent.getActivity(this, 0 , intent,
|
|
|
+ PendingIntent.FLAG_IMMUTABLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ String channelId = getString(R.string.default_notification_channel_id);
|
|
|
+ Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
+
|
|
|
+ NotificationCompat.Builder notificationBuilder =
|
|
|
+ new NotificationCompat.Builder(this, channelId)
|
|
|
+
|
|
|
+ .setSmallIcon(R.drawable.ic_logo_notify)
|
|
|
+ .setColor(getResources().getColor(R.color.app_yellow_color))
|
|
|
+
|
|
|
+ .setContentTitle(title + "")
|
|
|
+ .setContentText(messageBody)
|
|
|
+ .setAutoCancel(true)
|
|
|
+ .setSound(defaultSoundUri)
|
|
|
+ .setContentIntent(pendingIntent);
|
|
|
+
|
|
|
+ NotificationManager notificationManager =
|
|
|
+ (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationChannel channel = new NotificationChannel(channelId,
|
|
|
+ "Channel human readable title",
|
|
|
+ NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
+ notificationManager.createNotificationChannel(channel);
|
|
|
+ }
|
|
|
+
|
|
|
+ notificationManager.notify(App.NOTIFICATION_ID , notificationBuilder.build());
|
|
|
+ }
|
|
|
+}
|