Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package com.smedialink.dk_camp.domain.service;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.support.annotation.Keep;
  8. import android.support.v4.app.NotificationCompat;
  9.  
  10. import com.google.firebase.messaging.FirebaseMessagingService;
  11. import com.google.firebase.messaging.RemoteMessage;
  12. import com.smedialink.dk_camp.DkCampApplication;
  13. import com.smedialink.dk_camp.R;
  14. import com.smedialink.dk_camp.presentation.activity.authorization.LoginActivity;
  15. import com.smedialink.dk_camp.presentation.activity.menu.MenuActivity;
  16.  
  17. /**
  18.  * Created by Oleg Shelyakin on 08/02/17.
  19.  */
  20.  
  21. @Keep
  22. public class NotificationService extends FirebaseMessagingService {
  23.  
  24.     private final static Integer REQUEST_CODE = 2001;
  25.     private IUserSessionService userSessionService;
  26.  
  27.     @Override
  28.     public void onCreate() {
  29.         super.onCreate();
  30.         userSessionService = DkCampApplication.get(this).getComponent().getSessionService();
  31.     }
  32.  
  33.     @Override
  34.     public void onMessageReceived(RemoteMessage remoteMessage) {
  35.         super.onMessageReceived(remoteMessage);
  36.         createNotification(remoteMessage.getNotification().getBody());
  37.     }
  38.  
  39.     private void createNotification(String messageBody) {
  40.         Intent notificationIntent;
  41.         if (userSessionService.isLogedIn()){
  42.             notificationIntent = new Intent(this, MenuActivity.class);
  43.         } else {
  44.             notificationIntent = new Intent(this, LoginActivity.class);
  45.         }
  46.  
  47.         notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  48.         notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  49.  
  50.         PendingIntent contentIntent = PendingIntent.getActivity(this,
  51.                 REQUEST_CODE, notificationIntent,
  52.                 PendingIntent.FLAG_CANCEL_CURRENT);
  53.  
  54.         NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
  55.                 .setSmallIcon(R.mipmap.ic_launcher)
  56.                 .setContentTitle(getString(R.string.app_name))
  57.                 .setContentText(messageBody)
  58.                 .setAutoCancel(true)
  59.                 .setDefaults(NotificationCompat.DEFAULT_ALL)
  60.                 .setContentIntent(contentIntent);
  61.  
  62.         NotificationManager notificationManager =
  63.                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  64.  
  65.         notificationManager.notify((int) System.currentTimeMillis(), mNotificationBuilder.build());
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement