单击推送通知时如何打开不同的活动?

时间:2015-10-14 09:50:38

标签: android android-intent android-notifications

在我的应用中,我使用第三方SDK进行聊天,当我收到聊天时,它会出现推送通知。我需要的是当我点击推送通知时它应该受密码保护(单独的活动),然后一旦密码正确,它应该打开聊天页面(第三方SDK)。 这是我的代码

public GcmIntentService() {
    super(Library.SENDER_ID);
}

public static final String TAG = "XXX";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // Post notification of received message.


            boolean handled = MXNotificationManager.preProcessMXNotification(getApplicationContext(), intent);
            if (handled) {
                // This is a moxtra message and it will be handled by moxtra
                if (intent.getBooleanExtra(MXNotificationManager.MOXTRA_MESSAGE_SHOW_NOTIFICATION, false)) {
                    String title = intent.getStringExtra(MXNotificationManager.MOXTRA_MESSAGE_TITLE_TEXT);
                    if (intent.hasExtra(MXNotificationManager.MOXTRA_MESSAGE_ALERT_SOUND)) {
                        String soundUrl = intent.getStringExtra(MXNotificationManager.MOXTRA_MESSAGE_ALERT_SOUND);
                        Log.d(TAG, "soundUrl = " + soundUrl);
                        Uri uri = Uri.parse(soundUrl);
                        sendMoxtraNotification(title, uri, intent);

                    } else {
                        sendMoxtraNotification(title, intent);
                    }
                }
            }else {
                // Not a moxtra message and app should handle it.
                Log.i(TAG, "App should handle it.");

            }
            Log.i(TAG, "Received: " + extras.toString());
        }
    }

    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}


private void sendMoxtraNotification(String msg, Intent intent) {
    sendMoxtraNotification(msg, null, intent);
}

private void sendMoxtraNotification(String msg, Uri uri, Intent intent) {
    Log.d(TAG, "Got notification: msg = " + msg + ", uri = " + uri);
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);




   PendingIntent contentIntent = MXNotificationManager.getMXNotificationIntent(this, intent, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(getString(getApplicationInfo().labelRes))
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

    if (uri != null) {
        mBuilder.setSound(uri);
    }

    mBuilder.setContentIntent(contentIntent);
 mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

2 个答案:

答案 0 :(得分:1)

使用类似的东西

Intent notificationIntent = new Intent(getApplicationContext(), YourActivity.class);

//        notificationIntent.putExtra("message", msg);

        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

答案 1 :(得分:1)

基本上,您需要将PendingIntent从moxtra传递到您的密码活动。在通知中,您必须发送PendingIntent以显示密码活动。在密码活动中,如果用户输入正确的密码,您可以从moxtra发送PendingIntent。

以下是构建PendingIntent:

的代码段
    private void sendMoxtraNotification(String msg, Uri uri, Intent intent) {
    Log.d(TAG, "Got notification: msg = " + msg + ", uri = " + uri);
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent moxtraContentIntent = MXNotificationManager.getMXNotificationIntent(this, intent, 0);

    Intent passwordActivityIntent = new Intent(this, PasswordActivity.class);
    passwordActivityIntent.putExtra("PendingIntent", moxtraContentIntent);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, passwordActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(getString(getApplicationInfo().labelRes))
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

    if (uri != null) {
        mBuilder.setSound(uri);
    }

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

当用户输入正确的密码时,您必须:

PendingIntent intent = getIntent().getParcelableExtra("PendingIntent");
try {
    intent.send();
} catch (PendingIntent.CanceledException e) {
   e.printStackTrace();
}