我在服务中使用此代码,以便在我有任何新警报时收到通知,但是当我点击它们时,我没有进入我想要的视图:
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);
}
它显示但不可点击,所以这有什么不对吗?
答案 0 :(得分:1)
移动你的
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());
}
答案 1 :(得分:1)
把
mNotifyMgr.notify(mNotificationId, mBuilder.build());
后
mBuilder.setContentIntent(resultPendingIntent);