Created
July 26, 2017 14:51
-
-
Save anta40/c55b73884f6718b7d29450fa64b5282c to your computer and use it in GitHub Desktop.
Firebase messaging service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); | |
private NotificationUtil notificationUtil; | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
Log.e(TAG, "From: " + remoteMessage.getFrom()); | |
if (remoteMessage == null) | |
return; | |
// Check if message contains a notification payload. | |
if (remoteMessage.getNotification() != null) { | |
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); | |
handleNotification(remoteMessage.getNotification().getBody()); | |
} | |
// Check if message contains a data payload. | |
if (remoteMessage.getData().size() > 0) { | |
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); | |
try { | |
JSONObject json = new JSONObject(remoteMessage.getData().toString()); | |
handleDataMessage(json); | |
} catch (Exception e) { | |
Log.e(TAG, "Exception: " + e.getMessage()); | |
} | |
} | |
} | |
private void handleNotification(String message) { | |
if (!NotificationUtil.isAppIsInBackground(getApplicationContext())) { | |
// app is in foreground, broadcast the push message | |
Intent pushNotification = new Intent(NotificationConfig.PUSH_NOTIFICATION); | |
pushNotification.putExtra("message", message); | |
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); | |
// play notification sound | |
NotificationUtil notificationUtils = new NotificationUtil(getApplicationContext()); | |
notificationUtils.playNotificationSound(); | |
}else{ | |
// If the app is in background, firebase itself handles the notification | |
} | |
} | |
private void handleDataMessage(JSONObject json) { | |
Log.e(TAG, "push json: " + json.toString()); | |
try { | |
JSONObject data = json.getJSONObject("data"); | |
String title = data.getString("title"); | |
String message = data.getString("message"); | |
boolean isBackground = data.getBoolean("is_background"); | |
String imageUrl = data.getString("image"); | |
String timestamp = data.getString("timestamp"); | |
JSONObject payload = data.getJSONObject("payload"); | |
Log.e(TAG, "title: " + title); | |
Log.e(TAG, "message: " + message); | |
Log.e(TAG, "isBackground: " + isBackground); | |
Log.e(TAG, "payload: " + payload.toString()); | |
Log.e(TAG, "imageUrl: " + imageUrl); | |
Log.e(TAG, "timestamp: " + timestamp); | |
if (!NotificationUtil.isAppIsInBackground(getApplicationContext())) { | |
// app is in foreground, broadcast the push message | |
Intent pushNotification = new Intent(NotificationConfig.PUSH_NOTIFICATION); | |
pushNotification.putExtra("message", message); | |
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); | |
// play notification sound | |
NotificationUtil notificationUtil = new NotificationUtil(getApplicationContext()); | |
notificationUtil.playNotificationSound(); | |
} else { | |
// app is in background, show the notification in notification tray | |
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); | |
resultIntent.putExtra("message", message); | |
// check for image attachment | |
if (TextUtils.isEmpty(imageUrl)) { | |
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent); | |
} else { | |
// image is present, show notification with image | |
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl); | |
} | |
} | |
} catch (JSONException e) { | |
Log.e(TAG, "JSON Exception: " + e.getMessage()); | |
} catch (Exception e) { | |
Log.e(TAG, "Exception: " + e.getMessage()); | |
} | |
} | |
/** | |
* Showing notification with text only | |
*/ | |
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { | |
notificationUtil = new NotificationUtil(context); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
notificationUtil.showNotificationMessage(title, message, timeStamp, intent); | |
} | |
/** | |
* Showing notification with text and image | |
*/ | |
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { | |
notificationUtil = new NotificationUtil(context); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
notificationUtil.showNotificationMessage(title, message, timeStamp, intent, imageUrl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment