Open application after clicking on Notification

后端 未结 11 2115
南笙
南笙 2020-11-22 13:52

I have a notification in my app with the following code:

//Notification Start

   notificationManager = (NotificationManager) getSystemService(Context.NOTIFI         


        
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:54

    This is the way that I have approached.

    public class AppFCMService extends FirebaseMessagingService {
    
        private final static String TAG = "FCM Message";
        String notify, requstId, Notification;
        PendingIntent pendingIntent;
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
            //split string and getting order id from notification
            String Str = remoteMessage.getNotification().getBody();
            String[] tmp;
            tmp = Str.split(" ");
            String temp1 = tmp[0];
            String temp2 = tmp[1];
            String id = tmp[2];
            notify = temp1 + " " + temp2;
            requstId = id;
            showNotification(remoteMessage.getNotification().getBody());
        }
    
        private void showNotification(String messageBody) {
            // check whether session has been initiated or not
            if (new SessionHelper(getApplicationContext()).isLoggedIn()) {
    
                if (notify.equalsIgnoreCase("Travel request")) {
    
                    Intent intent = new Intent(this, ViewTravelDetailsActivity.class);
                    intent.putExtra("TravelRequestID", requstId);
                    intent.putExtra("BackPress", "Notify");
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
    
                } else if (notify.equalsIgnoreCase("Timesheet replied")) {
    
                    Intent intent = new Intent(this, ViewReplyActivity.class);
                    intent.putExtra("timesheetActivityID", requstId);
                    intent.putExtra("BackPress", "Notify");
                    intent.putExtra("RealmData", "DeleteRealm");
                    intent.putExtra("isToday", "true");
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
                } else {
    
                    Intent intent = new Intent(this, Dashboard.class);
                    intent.putExtra("timesheetActivityID", requstId);
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
                }
            } else {
    
                Intent intent = new Intent(this, LoginActivity.class);
                intent.putExtra("timesheetActivityID", requstId);
                pendingIntent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_ONE_SHOT);
            }
    
            Bitmap notifyImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(notifyImage)
                    .setColor(Color.parseColor("#FFE74C3C"))
                    .setContentTitle("TEST")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
    

提交回复
热议问题