Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package info.androidhive.firebasenotifications.service;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.support.v4.content.LocalBroadcastManager;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8.  
  9. import com.google.firebase.messaging.FirebaseMessagingService;
  10. import com.google.firebase.messaging.RemoteMessage;
  11.  
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import info.androidhive.firebasenotifications.activity.MainActivity;
  16. import info.androidhive.firebasenotifications.app.Config;
  17. import info.androidhive.firebasenotifications.util.NotificationUtils;
  18.  
  19. /**
  20.  * Created by Ravi Tamada on 08/08/16.
  21.  * www.androidhive.info
  22.  */
  23. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  24.  
  25.     private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
  26.  
  27.     private NotificationUtils notificationUtils;
  28.  
  29.     @Override
  30.     public void onMessageReceived(RemoteMessage remoteMessage) {
  31.         Log.e(TAG, "From: " + remoteMessage.getFrom());
  32.  
  33.         if (remoteMessage == null)
  34.             return;
  35.  
  36.         // Check if message contains a notification payload.
  37.         if (remoteMessage.getNotification() != null) {
  38.             Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
  39.             handleNotification(remoteMessage.getNotification().getBody());
  40.         }
  41.  
  42.         // Check if message contains a data payload.
  43.         if (remoteMessage.getData().size() > 0) {
  44.             Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
  45.  
  46.             try {
  47.                 JSONObject json = new JSONObject(remoteMessage.getData().toString());
  48.                 handleDataMessage(json);
  49.             } catch (Exception e) {
  50.                 Log.e(TAG, "Exception: " + e.getMessage());
  51.             }
  52.         }
  53.     }
  54.  
  55.     private void handleNotification(String message) {
  56.         if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
  57.             // app is in foreground, broadcast the push message
  58.             Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  59.             pushNotification.putExtra("message", message);
  60.             LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  61.  
  62.             // play notification sound
  63.             NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
  64.             notificationUtils.playNotificationSound();
  65.         }else{
  66.             // If the app is in background, firebase itself handles the notification
  67.         }
  68.     }
  69.  
  70.     private void handleDataMessage(JSONObject json) {
  71.         Log.e(TAG, "push json: " + json.toString());
  72.  
  73.         try {
  74.             JSONObject data = json.getJSONObject("data");
  75.  
  76.             String title = data.getString("title");
  77.             String message = data.getString("message");
  78.             boolean isBackground = data.getBoolean("is_background");
  79.             String imageUrl = data.getString("image");
  80.             String timestamp = data.getString("timestamp");
  81.             JSONObject payload = data.getJSONObject("payload");
  82.  
  83.             Log.e(TAG, "title: " + title);
  84.             Log.e(TAG, "message: " + message);
  85.             Log.e(TAG, "isBackground: " + isBackground);
  86.             Log.e(TAG, "payload: " + payload.toString());
  87.             Log.e(TAG, "imageUrl: " + imageUrl);
  88.             Log.e(TAG, "timestamp: " + timestamp);
  89.  
  90.  
  91.             if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
  92.                 // app is in foreground, broadcast the push message
  93.                 Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
  94.                 pushNotification.putExtra("message", message);
  95.                 LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
  96.  
  97.                 // play notification sound
  98.                 NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
  99.                 notificationUtils.playNotificationSound();
  100.             } else {
  101.                 // app is in background, show the notification in notification tray
  102.                 Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
  103.                 resultIntent.putExtra("message", message);
  104.  
  105.                 // check for image attachment
  106.                 if (TextUtils.isEmpty(imageUrl)) {
  107.                     showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
  108.                 } else {
  109.                     // image is present, show notification with image
  110.                     showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
  111.                 }
  112.             }
  113.         } catch (JSONException e) {
  114.             Log.e(TAG, "Json Exception: " + e.getMessage());
  115.         } catch (Exception e) {
  116.             Log.e(TAG, "Exception: " + e.getMessage());
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Showing notification with text only
  122.      */
  123.     private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
  124.         notificationUtils = new NotificationUtils(context);
  125.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  126.         notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
  127.     }
  128.  
  129.     /**
  130.      * Showing notification with text and image
  131.      */
  132.     private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
  133.         notificationUtils = new NotificationUtils(context);
  134.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  135.         notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement