Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package com.example.t00528583.firebasecloudmessage;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.media.RingtoneManager;
  9. import android.net.Uri;
  10. import android.os.IBinder;
  11. import android.support.v4.app.NotificationCompat;
  12. import android.support.v4.content.LocalBroadcastManager;
  13. import android.util.Log;
  14. import android.widget.TextView;
  15.  
  16. import com.google.firebase.messaging.FirebaseMessagingService;
  17. import com.google.firebase.messaging.RemoteMessage;
  18.  
  19. import static com.google.android.gms.internal.zzs.TAG;
  20.  
  21. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  22. public MyFirebaseMessagingService() {
  23. }
  24.  
  25.  
  26. @Override
  27. public void onMessageReceived(RemoteMessage remoteMessage) {
  28. // TODO(developer): Handle FCM messages here.
  29. // If the application is in the foreground handle both data and notification messages here.
  30. // Also if you intend on generating your own notifications as a result of a received FCM
  31. // message, here is where that should be initiated. See sendNotification method below.
  32. Log.d(TAG, "From: " + remoteMessage.getFrom());
  33. Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("message"));
  34. if(remoteMessage.getNotification() == null)
  35. sendNotification(remoteMessage.getData().get("message"), remoteMessage.getData().get("title"), remoteMessage.getData().get("subtitle"));
  36. else{
  37. String subtitle = "";
  38. sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(),subtitle);
  39. }
  40. }
  41.  
  42.  
  43. private void sendNotification(String messageBody, String messageTitle, String messageSubTitle) {
  44. Intent intent = new Intent(this, MainActivity.class);
  45. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  46. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  47. PendingIntent.FLAG_ONE_SHOT);
  48.  
  49. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  50. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  51. .setSmallIcon(android.R.drawable.ic_dialog_alert)
  52.  
  53. .setContentTitle(messageTitle)
  54. .setSubText(messageSubTitle)
  55. .setContentText(messageBody)
  56. .setAutoCancel(true)
  57. .setSound(defaultSoundUri)
  58. .setContentIntent(pendingIntent);
  59.  
  60. NotificationManager notificationManager =
  61. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  62.  
  63. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement