Android通知点击无效

4

我正在服务中使用这段代码,以便在有新警报时收到通知,但当我点击它们时,我没有进入想要的视图:

if(newAlertCounter > 0) // alert user about new alerts
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_action_warning)
    .setContentTitle(newAlertCounter + " new flood alerts!")
    .setContentText("Tap to see where in your vecinity.");

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

    // notification click action
    Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
}

它出现了,但是不能点击,那么问题出在哪里呢?
2个回答

2

将您的

mNotifyMgr.notify(mNotificationId, mBuilder.build());

之后

mBuilder.setContentIntent(resultPendingIntent);

当您调用.build()时,您将创建没有content intent的通知。(不会在之后添加,因为将发送到通知系统的对象将是Notification而不是Builder


if(newAlertCounter > 0) // alert user about new alerts
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_action_warning)
    .setContentTitle(newAlertCounter + " new flood alerts!")
    .setContentText("Tap to see where in your vecinity.");

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.


    // notification click action
    Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);

    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

2

放置

mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

之后

mBuilder.setContentIntent(resultPendingIntent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接