在通知中将 Activity 作为参数传递
Passing Activity as Parameter in Notification
我将通知定义为:
private void sendNotification(String msg, Activity onClickActivity) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
// Send to the login activity, since if already a member and logged in, should be able to start that activity
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, onClickActivity), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("Hello")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
如您所见,我希望能够允许动态引用 PendingIntent,以便用户可以单击通知并随后访问给定 Activity onClickActivity
的不同活动。目前错误说明它无法解析构造函数。
我怎样才能让它工作?
Intent
constructor 需要 class
,而不是 instance
。只需将您的方法签名更改为
private void sendNotification(String msg, Class<? extends Activity> onClickActivity) {
我将通知定义为:
private void sendNotification(String msg, Activity onClickActivity) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
// Send to the login activity, since if already a member and logged in, should be able to start that activity
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, onClickActivity), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("Hello")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
如您所见,我希望能够允许动态引用 PendingIntent,以便用户可以单击通知并随后访问给定 Activity onClickActivity
的不同活动。目前错误说明它无法解析构造函数。
我怎样才能让它工作?
Intent
constructor 需要 class
,而不是 instance
。只需将您的方法签名更改为
private void sendNotification(String msg, Class<? extends Activity> onClickActivity) {