我正在开发一个小应用,我希望后台服务发送通知,当用户点击通知时,它必须启动相应的活动。我在这里发布通知的代码,我的问题是通知显示在状态栏上但是当我点击它时它不启动活动。有人可以建议我哪里出错了。请帮忙。
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotificationService.this,
com.jeris.android.MetroActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(NotificationService.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(NotificationService.this, "Bullion",
"Rates for "+ parsedDate+"has not been updated. Go to app to check rates",
pendingIntent);
notificationManager.notify(11, notification);
答案 0 :(得分:5)
替换
PendingIntent.getService(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
使用
PendingIntent.getActivity(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
请注意 getService 已替换为 getActivity 。