Не удается добавить действия / кнопки в уведомление PU SH - PullRequest
0 голосов
/ 29 января 2020

Я использую Firebase Admin SDK (Java) для отправки уведомлений pu sh на Android устройства. Это все работает нормально.

Я ищу, чтобы добавить кнопку действия для уведомлений. Пример этого желаемого результата (для веб-пу sh)

Проблема

Я не могу найти ничего в SDK добавить действия к уведомлениям.

Кто-нибудь использовал действия для создания кнопок для уведомления pu sh?

Спасибо

1 Ответ

0 голосов
/ 29 января 2020

Попробуйте это в вашем Android приложении. Если вы хотите, вы можете создать намерение на основе информации, полученной из уведомления:

Для вас действительно важна часть, когда я создаю действие NotificationCompat.Action.

private void showPushNotification(RemoteMessage remoteMessage) {
    int requestCode = (int) (Math.random()*3000);
    int id = (int) (Math.random()*300000);
    String channelId = "fcm_defaul_channel_id"; // FIXME: set channelId
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Bitmap iconLarge = BitmapFactory.decodeResource(getResources(),
            R.drawable.googleg_standard_color_18);

    // title and body
    title = remoteMessage.getData().get("title"); //or remoteMessage.getNotification().getTitle()
    body = remoteMessage.getData().get("body"); or remoteMessage.getNotification().getBody()
    intent = new Intent(this, MainActivity.class);

    // create action send sms
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(
            0, "action test", pendingIntent ).build();

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.common_full_open_on_phone) // FIXME: change images
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(smsMessageIntent)
                    .addAction(action);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (notificationManager != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(id, notificationBuilder.build());
    } else { // FIXME: eliminare dopo i test
        Log.d(TAG, "notificationManager null");
        Toast.makeText(this, "notificationManager null", Toast.LENGTH_SHORT).show();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...