Clicking on notification doesn't open mentioned activity

后端 未结 10 1174
感动是毒
感动是毒 2020-12-09 16:26

I am trying to open an Activity when the notification is clicked and below is my code.

Intent intent = new Intent(this.getApplicationContext(),         


        
10条回答
  •  甜味超标
    2020-12-09 16:43

    I use this in my FirebaseMessagingService:

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String title, String messageBody, String data) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
    
        notificationManager.notify(id++ /* ID of notification */, notificationBuilder.build());
    }
    

提交回复
热议问题