Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
- @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) {
- handleNotification(remoteMessage.getNotification().getBody());
- showSimpleMsg(remoteMessage.getNotification());
- }
- // Check if message contains a data payload.
- if (remoteMessage.getData().size() > 0) {
- try {
- JSONObject json = new JSONObject(remoteMessage.getData());
- storeNotificationToPreferences(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),
- json);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private void showSimpleMsg(RemoteMessage.Notification notification) {
- Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
- pushNotification.putExtra("message", notification.getBody());
- LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
- showNotificationMessage(getApplicationContext(), notification.getTitle(), notification.getBody());
- }
- private void handleNotification(String message) {
- if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
- // app is in foreground, broadcast the push message
- Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
- pushNotification.putExtra("message", message);
- LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
- // play notification sound
- NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
- notificationUtils.playNotificationSound();
- } else {
- // If the app is in background, firebase itself handles the notification
- }
- }
- private void showNotificationMessage(Context context, String title, String message) {
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- String CHANNEL_ID = "olonce";
- NotificationChannel notificationChannel = null;
- notificationChannel = new NotificationChannel(CHANNEL_ID, "olonce", NotificationManager.IMPORTANCE_LOW);
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
- Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
- .setContentText(message)
- .setContentTitle(title)
- .setContentIntent(pendingIntent)
- .setChannelId(CHANNEL_ID)
- .setSmallIcon(R.mipmap.ic_launcher)
- .build();
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.createNotificationChannel(notificationChannel);
- notificationManager.notify(1, notification);
- } else {
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
- Notification notification = new Notification.Builder(getApplicationContext())
- .setContentText(message)
- .setContentTitle(title)
- .setContentIntent(pendingIntent)
- .setSmallIcon(R.mipmap.ic_launcher)
- .build();
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(1, notification);
- }
- }
- private void storeNotificationToPreferences(String title, String message,JSONObject extraData) {
- SharedPreferences preferences = getApplicationContext().getSharedPreferences("Notifications", MODE_PRIVATE);
- try {
- //Default Data
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("data", new JSONArray());
- jsonObject.put("unread", 0);
- JSONObject oldData = new JSONObject(preferences.getString("notifications", jsonObject.toString()));
- JSONArray data = oldData.getJSONArray("data");
- data.put(new JSONObject().put("title", title).put("message", message).put("data",extraData));
- int unread = (oldData.has("unread") ? oldData.getInt("unread") : 0) + 1;
- oldData.put("unread", unread);
- SharedPreferences.Editor editor = preferences.edit();
- editor.remove("notifications");
- editor.putString("notifications", oldData.toString());
- editor.commit();
- EventBus.getDefault().post(Common.EVENTS.NOTIFICATION);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement