单击GCM通知时打开活动

时间:2014-07-28 02:45:06

标签: android google-cloud-messaging

我正在制作gcm应用程序,现在我可以收到通知

但是当我点击通知时,它只是打开应用程序。

我需要打开另一个活动而不是Mainactivity

有没有办法做到这一点?

4 个答案:

答案 0 :(得分:2)

final Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("key", "value");
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.set...;
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

答案 1 :(得分:1)

有关创建通知http://developer.android.com/guide/topics/ui/notifiers/notifications.html的详细信息,请阅读此内容。
取决于http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse,从通知中启动活动有两种常见情况 - 常规活动和特殊活动。
以下方法是开始2类活动的例子:

常规活动

    private static final int NOTIFICATION_ID = 602;
    private static final int REQUEST_CODE_START_ACTIVITY = 610;
    /**
     * Create and show a simple notification containing the received GCM message.
     */
    private void sendNotification(String title, String message, Intent intent) {
        // Create a start PendingIntent
        PendingIntent resultPendingIntent = null;
        ComponentName componentName = intent.getComponent();
        if (componentName != null) {
            // The stack builder object will contain an artificial back
            // stack for the started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack for the Intent (but not the Intent itself) <== This comment must be wrong!
            stackBuilder.addParentStack(componentName);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(intent);

            resultPendingIntent = stackBuilder.getPendingIntent(REQUEST_CODE_START_ACTIVITY, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        } else {
            resultPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_START_ACTIVITY, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        }


        // Notification properties
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setContentIntent(resultPendingIntent);

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

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

    }

用法:

    Intent intent = new Intent(this, SignInActivity_.class);
    sendNotification(TextUtils.isEmpty(title) ? getString(R.string.app_name) : title, message, intent);

清单XML应该如下所示

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SignInActivity_"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

特别活动

    private static final int NOTIFICATION_ID = 602;
    private static final int REQUEST_CODE_START_ACTIVITY = 610;
    /**
     * Create and show a simple notification containing the received GCM message.
     */
    private void sendNotification(String title, String message, Intent intent) {
        // Create a start PendingIntent
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_START_ACTIVITY, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        // Notification properties
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setContentIntent(resultPendingIntent);

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

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

    }

注意: 由于我使用setDefaults(DEFAULT_ALL),因此振动权限需要<uses-permission android:name="android.permission.VIBRATE" />

答案 2 :(得分:0)

是的,这是可能的。您必须将manifest.xml中活动的“exported”标志设置为true。

希望它有所帮助。

答案 3 :(得分:0)

使用此:

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(context,YourActivity.class);
PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification;
    if (Build.VERSION.SDK_INT < 11) {
        notification = new Notification(icon, "Title", when);
        notification.setLatestEventInfo(
                context,
                "Title",
                "Text",
                pending);
    } else {
        notification = new Notification.Builder(context)
                .setContentTitle("Title")
                .setContentText(
                        "Text").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pending).setWhen(when).setAutoCancel(true)
                .build();
    }
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
nm.notify(0, notification);