Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.t00528583.firebasecloudmessage;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.IBinder;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import android.widget.TextView;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import static com.google.android.gms.internal.zzs.TAG;
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- public MyFirebaseMessagingService() {
- }
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- // TODO(developer): Handle FCM messages here.
- // If the application is in the foreground handle both data and notification messages here.
- // Also if you intend on generating your own notifications as a result of a received FCM
- // message, here is where that should be initiated. See sendNotification method below.
- Log.d(TAG, "From: " + remoteMessage.getFrom());
- Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("message"));
- if(remoteMessage.getNotification() == null)
- sendNotification(remoteMessage.getData().get("message"), remoteMessage.getData().get("title"), remoteMessage.getData().get("subtitle"));
- else{
- String subtitle = "";
- sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(),subtitle);
- }
- }
- private void sendNotification(String messageBody, String messageTitle, String messageSubTitle) {
- Intent intent = new Intent(this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
- PendingIntent.FLAG_ONE_SHOT);
- Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
- .setSmallIcon(android.R.drawable.ic_dialog_alert)
- .setContentTitle(messageTitle)
- .setSubText(messageSubTitle)
- .setContentText(messageBody)
- .setAutoCancel(true)
- .setSound(defaultSoundUri)
- .setContentIntent(pendingIntent);
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement