0

有没有办法重定向 - 当用户单击通知 ui 中的通知时 - 到另一个活动?

例如

我在 Google 日历中创建了一个活动,当有通知时,当用户单击它时,它将打开我的活动,而不是直接转到 Google 日历。

4

1 回答 1

0

尝试这个 :

private void generateNotification(Context context, String message,
long when, String query) {
int icon = R.drawable.icon;
 long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;

Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    intent, 0);

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
    notification = new Notification(icon, message, when);
    notification.setLatestEventInfo(context, appname, message,
            contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify((int) when, notification);

} else {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    notification = builder.setContentIntent(contentIntent)
            .setSmallIcon(icon).setTicker(appname).setWhen(when)
            .setAutoCancel(true).setContentTitle(appname)
            .setContentText(message).build();

    notificationManager.notify((int) when, notification);

}

希望这可以帮助。

于 2013-10-28T04:24:37.293 回答