Перейти к конкретной деятельности, когда уведомление получено в закрытом приложении? - PullRequest
0 голосов
/ 18 декабря 2018

Ниже приведен мой код, он отлично работает, когда приложение находится в фоновом режиме, но он получает заставку, когда приложение не в фоновом режиме;

 Intent resultIntent = null;
    if (flag.equalsIgnoreCase("refer_friend")) {
        resultIntent = new Intent(getApplicationContext(), MyLoyaltyHistoryActivity.class);
        resultIntent.putExtra("key", "2");
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(resultIntent);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(getApplicationContext());
    mBuilder.setSmallIcon(R.drawable.yu_android);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setSmallIcon(R.drawable.yu_android);
    } else {
        mBuilder.setSmallIcon(R.drawable.yu_android);
    }


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_MAX;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

Ответы [ 2 ]

0 голосов
/ 19 декабря 2018

Наконец я получил свой ответ, используя TaskStackBuilder

Используемый ниже код;

  Intent resultIntent = null;

    if (flag.equalsIgnoreCase("refer_friend")) {
        resultIntent = new Intent(getApplicationContext(), ReferFreindLoyaltyActivity.class);
        resultIntent.putExtra("back_flag", "1");
        resultIntent.putExtra("refer_a_freind", "1");
    }

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(getApplicationContext());
    mBuilder.setSmallIcon(R.drawable.yu_android);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent)
            .setAutoCancel(true);

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setSmallIcon(R.drawable.yu_android);
    } else {
        mBuilder.setSmallIcon(R.drawable.yu_android);
    }


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_MAX;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
0 голосов
/ 18 декабря 2018

Если ваше приложение не работает в фоновом режиме, вы можете получить данные JSON, которые были отправлены в полезных данных, с уведомлением следующим образом:

Просто добавьте эту строку кода в ваш SplashActivity.java :

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String pushType = bundle.getString("push_type");

Внутри пакета вы получите все данные о значении ключа, которые вы отправляли из своего бэкэнда.Здесь, в моем случае, я посылаю ключ push_type в полезную нагрузку данных, полученную в моем случае, вы также можете получить свой ключ здесь в SplashActivity.java .

...