web-dev-qa-db-ja.com

タップされたプッシュ通知で特定のアクティビティを開く方法は?

さて、私が今していることは、順調に進んでいるFCMを介してプッシュ通知を受け取ることです。アプリケーションがフォアグラウンドにあるときにアクティビティを変更できるようになりましたが、通知パネルで通知をタップしたときにアクティビティを変更するにはどうすればよいですか?助けが必要。

私のコード:

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {

        // Check for empty Push message
        if (TextUtils.isEmpty(message))
            return;

        // notification icon
        final int icon = R.mipmap.ic_launcher;

        // on click activity for the notification !!!!!!!!!!
        intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(mContext, TestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext);
4
Ahsan Arif
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    long notificatioId = System.currentTimeMillis();

    Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect.

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);

    int currentapiVersion = Android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= Android.os.Build.VERSION_CODES.Lollipop){
        currentapiVersion = R.mipmap.ic_notification_lolipop;
    } else{
        currentapiVersion = R.mipmap.ic_launcher;
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(currentapiVersion)
            .setContentTitle(this.getResources().getString(R.string.app_name))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_HIGH)
            .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setContentIntent(contentIntent);
    mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
}
5
Bhavnik
Pass your Activity you want to open when clicked into Intent.
Intent notificationIntent = new Intent(context, XYZActivity.class);

complete code..
    NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        Intent notificationIntent = new Intent(context, XYZActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
0
Satyam Anand
<!-- MainActivity is the parent for ResultActivity -->
    <activity
        Android:name=".ResultActivity"
       />

子アクティビティ宣言でマニフェストを調整することを忘れないでください

0
Waqas N
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(mContext,ACTIVITY_TO_BE_DISPLAYED.class); // Replace ACTIVITY_TO_BE_DISPLAYED to Activity to which you wanna show
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setAutoCancel(true)
                .setTicker("YOUR_TICKER_MSG")
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setLargeIcon(icon)
                .setContentTitle("YOUR_TITLE")
                .setContentText("YOUR_TEXT")
                .setContentIntent(intent);
        notificationManager.notify(10, builder.build());
0
Mukeshkumar S