【问题标题】:FCM Push notification with user's avatar带有用户头像的 FCM 推送通知
【发布时间】:2019-04-04 19:56:12
【问题描述】:

我正在尝试创建一个推送通知,例如 WhatsApp 或 Gmail,其中用户头像出现在通知中。有没有办法在 react-native 中做到这一点,尤其是使用 expo?

这是我的 fcm 负载

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

这是我从 Google 获得的一个我想要实现的示例。

【问题讨论】:

    标签: react-native expo


    【解决方案1】:

    答案(来源):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

    【讨论】:

    • 啊!所以通知应该来自本地通知?有没有办法通过远程通知做到这一点?我正在使用expo,所以可能会有一些限制。
    • 抱歉,这只是变量的名称。这也适用于远程通知。只需设置 react-native-firebase (github.com/invertase/react-native-firebase)
    • 嘿@JasperMartin 你知道我如何在反应原生onesignal中实现这一点吗?
    • 嘿@JasperMartin 你知道我如何在反应原生onesignal中实现这一点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多