Добавление кнопки действия в уведомление Android

#android #android-intent #notifications #android-actionbar #alert

#Android #android-намерение #уведомления #android-панель действий #предупреждение

Вопрос:

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

Я знаю, как добавлять кнопки действий в целом, но как я могу добавить кнопку действия в уведомление в этом коде..

нужна помощь .. заранее спасибо..

Код здесь ..

    public void onReceive(Context context, Intent intent)
        {
            MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.alert);
            mPlayer.start();
            mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            CharSequence from = intent.getStringExtra("Name");
            CharSequence message = intent.getStringExtra("Description");
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
            notification = new Notification(R.drawable.alert, "Notification", System.currentTimeMillis());
            notification.setLatestEventInfo(context, from, message, contentIntent);
            mNotificationManager.notify(Integer.parseInt(intent.getExtras().get("NotifyCount").toString()), notification);
            Toast.makeText(context, "New Notification Received", Toast.LENGTH_LONG).show();
        }
 

Комментарии:

1. Это значит, что вам нужна кнопка в панели уведомлений?

2. да, мне нужно добавить кнопку в уведомление

3. Джия, я думаю, тебе нужно создать там пользовательскую кнопку

4. У вас есть что-то, чего нет у @Sparrow?

5. Джия, ты получила мое сообщение или нет??

Ответ №1:

 public void onReceive(Context context, Intent intent)
    {
        MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.alert);
        mPlayer.start();
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence from = intent.getStringExtra("Name");
        CharSequence message = intent.getStringExtra("Description");
        PendingIntent contentIntent =      PendingIntent.getActivity(context, 0, new Intent(), 0);
        notification = new Notification(R.drawable.alert, "Notification", System.currentTimeMillis());
        notification.addAction(R.mipmap.yes, "Acion_Name", contentIntent);
        mNotificationManager.notify(Integer.parseInt(intent.getExtras().get("NotifyCount").toString()), notification);
       Toast.makeText(context, "New Notification Received", Toast.LENGTH_LONG).show();
    }
 

Так что вам просто нужно добавить .addAction
Эта строка кода, где, где вы будете вызывать свое ожидающее намерение:

 notification.addAction(R.mipmap.yes, "Acion_Name", contentIntent);