Не могу добавить действия/кнопки в PUSH-уведомление

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

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

Проблема

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

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

Спасибо


person dgallagher_ire    schedule 29.01.2020    source источник


Ответы (1)


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

Что действительно важно для вас, так это та часть, когда я создаю 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();
    }
person AM13    schedule 29.01.2020
comment
Спасибо за ответ. Я использую SDK на стороне сервера. Пытаюсь выяснить, как я могу управлять действиями (если возможно) от уведомления до приложения. Вот как это делается [Web Push[(пример firebase.googleblog.com/2018/05/). Ваш код работает в приложении, я хочу, чтобы действие/кнопка отображалась на основе кода на стороне сервера для создания push-сообщения. - person dgallagher_ire; 30.01.2020