如何将firebase通知用于Android Oreo


    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
    Log.e(TAG, "From: " + remoteMessage.getFrom());
    if (remoteMessage == null)
        return;
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "ProfileResp Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject data = new JSONObject(remoteMessage.getData());
            String title = data.getString("title");
            String message = data.getString("message");
            //imageUri will contain URL of the image to be displayed with Notification
            String imageUri = remoteMessage.getData().get("trailer_img");
            //To get a Bitmap image from the URL received
            bitmap = getBitmapfromUrl(RestURLs.BASE_URL+imageUri);
            sendNotification(title, message, bitmap);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}
/*
 *To get a Bitmap image from the URL received
 * */
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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}
// [END receive_message]
/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String ttl, String messageBody, Bitmap image) {
    Intent intent = new Intent(this, ActivityHome.class);
    intent.putExtra("title", ttl);
    intent.putExtra("message", messageBody);
    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)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(ttl)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

这是我的代码。通过使用此代码,通知显示在所有手机中, 但不在奥利奥手机中。我更新了我的firbase依赖性。但仍然不起作用

我也使用了频道,但我不知道如何将其与Firebase一起使用。任何人都可以帮助我收到有关基于Oreo的手机的通知。

自Android O,Google添加NotificationChannel

演示使用渠道按主题对通知进行分类。此功能已在Android O中添加,并允许用户对其通知首选项具有细粒度的控制。

使用此方法返回NotificationCompat.Builder

public NotificationCompat.Builder initChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return new NotificationCompat.Builder(context);
    }
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("default",
            "Channel name",
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");
    notificationManager.createNotificationChannel(channel);
    return new NotificationCompat.Builder(context, "default");
}

并确保使用最新的支持库。

最新更新