java - Android 通知未启动 Activity

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

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

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);

现在我的通知工作了!

最佳答案

试试这个:(您需要指定 Activity 启动 Intent ,您应该将其放入待处理 Intent 中)

请阅读以下指南以从头开始创建工作通知: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());

关于java - Android 通知未启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473373/

相关文章:

java - Wso2 5.8,具有 3 个身份验证步骤

android - HTC 设备上的 imeoptions

java - 如何将对象树传递到包中?

java - Android Activity 管理器 REAL_GET_TASKS 错误

java - 使用 Jersey Tomcat 服务获取 404 错误

java - 安卓语言本地化

java - Eclipse RCP 是否包含内置的剪切/复制/粘贴处理程序?

android - 在 Play 商店中发布适用于手机和 watch 的 Android 应用

android - ListView 中每一列的单独点击事件

android - 在 Android 中更改 Activity - List inside Tab