【问题标题】:FCM notification like instagramFCM 通知,如 instagram
【发布时间】:2018-06-13 21:31:24
【问题描述】:

我已在我的应用中实施 FCM,并从应用中执行的操作中获取通知。因此,如果用户喜欢帖子,我会收到类似“某人(姓名)喜欢你的通知。如果多个用户喜欢帖子,我会收到多个通知 exe:用户 1 喜欢你,用户 2 喜欢你。

我想要一个通知,其中显示用户的用户名 + 喜欢你的其他用户的数量。如何在客户端处理这个问题,或者我们必须从服务器端处理它。有什么想法吗?

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    Firebase 云消息让您可以实时向用户发送消息。我建议你从谷歌官方文档中阅读它,然后实施它,以便当有人喜欢你的帖子时,你的服务器能够发送 JSON 消息来激活 Firebase Cloud Messaging。

    通知有两种形式:

    • 通知消息,有时被认为是“显示消息”。 这些由 FCM(firebase 云消息传递)SDK 处理 自动地。
    • 数据消息,由客户端应用处理。

    第一步:将 FCM SDK API 添加到您的项目中

    第二步创建一个可以接收您的数据消息的服务

    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
       private static final String TAG = "myTag";
       @Override
       public void onMessageReceived(RemoteMessage remoteMessage) {
     Log.d(TAG, "From: " + remoteMessage.getFrom());
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        }
    }
    

    第三步:从该服务创建通知

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Someone liked your post")
        .setContentText("Accept this answer if it is useful!");
    

    第四步:通过向您的 Android 应用发送带有通知的 JSON 请求来处理服务器端的任何“点赞”:

    {
      "message":{
        "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", //this specefies the device
        "notification":{
          "title":"Someone liked your post",
          "body":"col"
        }
    "data": {
          "Nick" : "Mario",
          "Room" : "PortugalVSDenmark"
        }
     "android":{
           "ttl":"86400s",
           "notification"{
             "click_action":"OPEN_ACTIVITY_1"
           }
         },
      }
    }
    

    在我的blog 上了解有关 Firebase 云消息传递的更多信息。

    【讨论】:

    • 我收到了正确的通知,但执行了一项操作的通知,我想要执行多项操作的通知前:20 人喜欢,所以通知将是:某人 + 19 人喜欢你。
    • 图片呢?
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多