单击“通知”时无法导航到其他活动?

时间:2018-01-07 18:14:53

标签: android push-notification notifications

当我点击通知时,我应该能够导航到第二个活动,但我无法导航到第二个活动(Main2Activity)。

请告诉我这是什么错误,下面是我的代码:

        public void onClick(View view) {

            NotificationCompat.Builder  noti = new 
            NotificationCompat.Builder(MainActivity.this);
            noti.setContentTitle("Message for you!");
            noti.setContentText("Hi!!This is message for you");
            noti.setSmallIcon(R.drawable.ic_launcher_background);
            noti.setTicker("app name:message app");
            noti.setAutoCancel(true);
            Notification notification=noti.build();

            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(MainActivity.this);
            taskStackBuilder.addParentStack(Main2Activity.class);
            taskStackBuilder.addNextIntent(intent);
            PendingIntent pendingIntent= taskStackBuilder.getPendingIntent(1234,PendingIntent.FLAG_UPDATE_CURRENT);
            noti.setContentIntent(pendingIntent);


            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1234,notification);

1 个答案:

答案 0 :(得分:0)

Notification notification=noti.build();

上面的行是错误的,应该写在

之后

noti.setContentIntent(pendingIntent);

一旦完成上述代码就可以正常工作。

由于