Дополнительный ноль для уведомлений Android с PendingIntent - PullRequest
0 голосов
/ 20 октября 2019

Я пытаюсь отправить int Extra в Activity, когда происходит уведомление. Когда я нажимаю на уведомление, действие запускается, но дополнительные всегда равны нулю.

Я проверил много похожих вопросов, но они не помогли мне решить проблему. (Я пытался использовать onNewIntent (), но он никогда не вызывался). Как я могу успешно отправить Extra?

Это BroadcastReceiver, где создается намерение:

public class EventAlarmNearReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent nextIntent = new Intent().setClass(context, EventActivity.class);
        nextIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int id = 1234;
        nextIntent.putExtra("Id", (long) id);
        nextIntent.setAction("nearReceiverAction");
        PendingIntent nextPendingIntent = PendingIntent.getActivity(context, 1022, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        String text = "Your " + intent.getStringExtra("Name") + " is starting soon. Please read your documents.";

        NotificationsDeliver.getInstance().sendNotification(context, "Meeting Mate", text, nextPendingIntent);
    }
}

Это метод из NotificationsDeliver, куда отправляется уведомление:

public void sendNotification(@NonNull Context context, String title, String text, PendingIntent pendingIntent ) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_event_note_white_24dp)
                .setContentTitle(title)
                .setContentText(text)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVibrate(new long[] { 1000, 1000, 1000 })
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(999, builder.build());
    }

Это метод onCreate из EventActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    long id = intent.getLongExtra("id", 0);
}

1 Ответ

1 голос
/ 20 октября 2019

Я бы предположил, что это потому, что вы ставили слово «id» по-другому, когда ставили vs, получая дополнительные;сравните:

nextIntent.putExtra("Id", (long) id);
//                  ^^^^

против

long id = intent.getLongExtra("id", 0);
//                            ^^^^

Попробуйте использовать одинаковую заглавную букву для обоих.

...