fcmMessage

FCM开发总结

2016-12-11  本文已影响0人  带土的旋律

Android端

先决条件

将Firebase添加至Android项目

添加SDK

首先在根级别的build.gradle文件添加一条规则。以包含Google服务插件

   // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }

然后在模块Gradle文件中,底部添加apply plugin行,以启用 Gradle 插件:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.6.1'
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

然后根据需求添加Firebase SDK依赖项。上面的com.google.firebase:firebase-core:9.6.1

上面的依赖firebase如果是从9.6.1升级到10.0.1,gcm也需要升级到10.0.1,否则会出现crash。

Firebase功能库完整列表:

Gradle 依赖项行 服务
com.google.firebase:firebase-core:9.6.1 Analytics
com.google.firebase:firebase-messaging:9.6.1 Cloud Messaging / Notifications
... ...

在Android studio 2.2+,上面的步骤可以用Tools->Firebase,然后根据提示实现集成。

设置FCM客户端

    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
    <!-- [START firebase_service] -->
    <service
        android:name="packagename.MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_service] -->
    <!-- [START firebase_iid_service] -->
    <service
        android:name="packageName.MyFirebaseInstanceIDService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_iid_service] -->

Server端

Server端实现步骤如下:

  1. 将Firebase添加至服务器

  2. 实现连接服务器协议

  3. 发送下游消息(云端至设备)

FCM有三种消息类型,分别为Notification message,Data Message,Messages with both notification and data payload

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key= App Key
{
    "notification" : {
      "body" : "You have a new message",
      "title" : "",
      "icon" : "myicon" // Here you can put your app icon name
    },
    "to" : "token..."
}

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key= App Key
{
    "data" : {
      "Nick" : "Obito",
      "xxx" : "xxx"
    },
    "to" : "token..."
}

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=App Key

{   
    "notification" : {
      "body" : "You have a new message",
      "title" : "",
      "icon" : "myicon" // Here you can put your app icon name
      "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
    }, 
    "data": {
    "Nick" : "Obito",
    "xxx" : "xxx"
  },
  "to" : "token..."
}

点击的时候在OPEN_ACTIVITY_1中拦截

<intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

遇到的一些问题

Firebase控制台测试只能发送Notification,测试的时候把App从最近列表划掉之后能收到,而且是在没翻墙的情况下都能收到。当然当进程被完全杀死就收不到了。data Message则需要通过server api调用,前台后台都能收到透传消息。

上一篇 下一篇

猜你喜欢

热点阅读