具有用户头像的FCM推送通知

时间:2019-02-27 01:40:09

标签: react-native expo

我正在尝试创建类似whatsApp或Gmail的推送通知,其中通知中包含用户头像。有没有办法在本机操作中做到这一点,尤其是使用expo?

这是我的fcm有效载荷

{
"GCM": "{ \"notification\": { \"title\": \"Sender1\" }, \"text\": \"test message\" } }"
}

这是我想从Google获得的示例。

enter image description here

1 个答案:

答案 0 :(得分:0)

答案(来源):How to set the app icon as the notification icon in the notification drawer 由用户@manikanta

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

android.app.NotificationManager notificationManager =
                (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

设置大号图标可以解决问题。如果您有其他信息,请在下面评论

如果您使用的是React Native(react-native-firebase):

const notif = new firebase.notifications.Notification({                                                                                                             
    show_in_foreground: true,                                                                                                                                                   
})
    .android.setSmallIcon('@mipmap/ic_notification') // app icon

// source image might be:
// URL
// android resource e.g. @mipmap/ic_launcher
let source_image = "";
notif.android.setLargeIcon(source_image) // user avatar

来源:https://rnfirebase.io/docs/v5.x.x/notifications/reference/AndroidNotification#setLargeIcon