Android Notification无法启动活动

时间:2014-08-24 15:39:53

标签: java android android-activity notifications google-cloud-messaging

我有问题。 当我点击通知时将被取消但不会打开活动。 这是代码:

GcmIntentService.java

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                con)
                .setAutoCancel(true)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_VIBRATE
                                | Notification.FLAG_AUTO_CANCEL)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(getText(R.string.app_name))
                .setContentText(msg);

        Intent resultIntent = new Intent(con, Notification.class);

        resultIntent.putExtra("message", msg);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(con);

        stackBuilder.addParentStack(Home.class);

        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
        mBuilder.setContentIntent(resultPendingIntent);


        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(1, mBuilder.build());

修改

我用这段代码解决了:

Intent intent = new Intent(this, Home.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);    
Notification noti = new Notification.Builder(this)
                .setContentTitle(getText(R.string.app_name))
                .setContentText(msg)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0, noti);

现在我的通知工作了!

1 个答案:

答案 0 :(得分:0)

尝试这样做:(您需要指定“活动启动意图”,您应将其置于待定意图中)

请阅读以下指南,从头开始创建工作通知:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

    // Intent that will launch activity
    Intent notifyIntent = new Intent(context, MainFragmentFaceActivity.class);

    // Creates the PendingIntent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Puts the PendingIntent into the notification builder
    notificationBuilder.setContentIntent(pendingIntent);

    notifyManager.notify(R.id.notification_move, notificationBuilder.build());