Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package premiumcard.app.services;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- 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 premiumcard.app.BaseApplication;
- import premiumcard.app.R;
- import timber.log.Timber;
- // FCM PushListener IMPLEMENTATION
- /*
- * This service is designed to run in the background and receive messages from gcm. If the app is in the foreground
- * when a message is received, it will immediately be posted. If the app is not in the foreground, the message will be saved
- * and a notification is posted to the NotificationManager.
- */
- public class MyFirebaseMessagingService extends FirebaseMessagingService
- {
- public static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
- /**
- * This method is 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.
- */
- public void onNewToken(String token) {
- try {
- super.onNewToken(token);
- Log.d(TAG, "FCM_Token: " + token);
- // TODO: Implement this method to send any registration to your app's servers.
- sendRegistrationToServer(token);
- } catch (Exception e) {
- Log.d(TAG, "Failed to complete token refresh", e);
- }
- }
- /**
- * Persist token to third-party servers.
- *
- * 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) {
- Timber.i("FB Token = " + token);
- // TODO: send the token to SNS and create platform endpoint
- }
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- // [START_EXCLUDE]
- /*There are two types of messages : data messages and notification messages.
- Data messages are handled here in onMessageReceived whether the app is in the foreground or background.
- Notification messages are only received here in onMessageReceived when the app is in the foreground.
- When the app is in the background, an automatically generated notification is displayed.
- Messages containing both notification and data payloads are treated as notification messages.
- The Firebase console always sends notification messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options*
- */
- // [STOP_EXCLUDE]
- // TODO(developer): Handle FCM messages here.
- // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
- Log.d(TAG, "From: " + remoteMessage.getFrom());
- // Check if message contains a data payload.
- Intent intent = new Intent(this, BaseApplication.getmInstance().activeActivity.getClass());
- if (remoteMessage.getData().size() > 0) {
- Log.d(TAG, "Message data payload: " + remoteMessage.getData());
- }
- // Check if message contains a notification payload.
- if (remoteMessage.getNotification() != null) {
- Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
- String messageBody = remoteMessage.getData().get("message");
- displayNotificationMessage(messageBody);
- }
- }
- /**
- * Create and show a simple notification containing the received FCM Notification message.
- *
- * @param messageBody FCM message body received.
- */
- public void displayNotificationMessage(String messageBody) {
- Intent intent = new Intent(this, BaseApplication.getmInstance().activeActivity.getClass());
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
- PendingIntent.FLAG_CANCEL_CURRENT);
- String channelId = getString(R.string.default_notification_channel_id);
- Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder =
- new NotificationCompat.Builder(this, channelId)
- .setContentTitle("TEst")
- .setContentText(messageBody)
- .setContentIntent(pendingIntent)
- .setAutoCancel(true)
- .setSound(defaultSoundUri)
- .setContentIntent(pendingIntent)
- .setColor(Color.parseColor("#3F5996"));
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- // Since android Oreo notification channel is needed.
- 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(0 /* ID of notification */, notificationBuilder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement