Android Firebase Pu sh Уведомление с изображением не работает - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь отправить уведомление firebase pu sh с изображением в качестве полезной нагрузки. Я получаю уведомление, но, к сожалению, не вижу изображения в своем уведомлении. Я использую приведенный ниже код для достижения результата.

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FirebaseMessageService";
    Bitmap bitmap;

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            String imageUri = remoteMessage.getData().get("image");
            bitmap = getBitmapfromUrl(imageUri);
        }


        if (remoteMessage.getNotification() != null){
            String title = remoteMessage.getNotification().getTitle(); //get title
            String message = remoteMessage.getNotification().getBody(); //get message
            String click_action = remoteMessage.getNotification().getClickAction(); //get click_action

            sendNotification(title, message, Objects.requireNonNull(click_action), bitmap);
        }
    }

    private void sendNotification(String title, String messageBody, String click_action, Bitmap image) {
        Intent intent;
        if(click_action.equals("VIDEOACTIVITY")){
            intent = new Intent(this, VideoActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else if(click_action.equals("PRODUCTACTIVITY")){
            intent = new Intent(this, ProductDetail.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else{
            intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(image)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

    public Bitmap getBitmapfromUrl(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(input);
            return bitmap;

        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
}

AndroidManifest

<service
   android:name=".FirebaseMessagingService"
   android:enabled="true"
   android:exported="true">
   <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

<meta-data
   android:name="com.google.firebase.messaging.default_notification_color"
   android:resource="@color/colorPrimary" />

Отправка уведомления с помощью Postman

{
    "to":
        "/topics/TEST"
        ,
    "data":{
        "image": "image_url"
    },
    "notification":{
        "title": "Notification title",
        "text": "message",
        "click_action": "VIDEOACTIVITY"
    }
}

Любая помощь будет принята с благодарностью.

...