Pending intent in notification not working

前端 未结 6 1932
感情败类
感情败类 2020-12-02 13:13

Below is my block of code which should open NotificationActivity when the notification is tapped on. But its not working.

private void setNo         


        
相关标签:
6条回答
  • 2020-12-02 13:42

    If the app is already running then you need to handle it in onNewIntent

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    
        //Handle intent here...
    }
    
    0 讨论(0)
  • 2020-12-02 13:46

    Try checking your AndroidManifest.xml, double check if you are navigating to an activity with an intent-filter main action, launcher category

    For example,

    I couldn't make my pending intent to navigate to HomeActivity but it works when navigating to SplashScreen

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 13:50

    You might be using a notification id equal to 0. There is known issue with notification id being 0. If you will use any other id, it should work.

    0 讨论(0)
  • 2020-12-02 13:50

    I added task builder and the below block code worked for me

    Intent intent = new Intent(context, HomeActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(SplashActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    
    0 讨论(0)
  • 2020-12-02 13:56

    try this:

    private void setNotification(String notificationMessage) {
    
    //**add this line**
    int requestID = (int) System.currentTimeMillis();
    
    Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
    
    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);
    
    //**add this line**
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    
    //**edit this line to put requestID as requestCode**
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.drawable.logo)
    .setContentTitle("My Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setContentText(notificationMessage).setAutoCancel(true);
    mBuilder.setSound(alarmSound);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    
    }
    
    0 讨论(0)
  • 2020-12-02 14:00

    Added the requestid, but the intent was still not opening.

    This is the solution that worked for me:

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    

    Setting those flags to clear the activities below the intent activity.

    0 讨论(0)
提交回复
热议问题