Android push fcm
远程推送(FCM)- For Developer
FCM官方文档: Firebase 云消息传递
谷歌官方已经把GCM设为deprecated,并在未来停止提供支持。为了应对这个变化,海外版本的LCM,远程推送需要从GCM(Google Cloud Messaging)迁移到最新的 FCM(Firebase Cloud Messaging),官方迁移文档:[Migrate a GCM Client App for Android to Firebase Cloud Messaging] (https://developers.google.com/cloud-messaging/android/android-migrate-fcm)
下面介绍游戏方接入 FCM(Firebase Cloud Messaging)需要的步骤:
1.需要有Google开发者账号(之前接过GCM的话直接用之前的),再登陆Firebase控制台(https://console.firebase.google.com/)将原有的GCM项目导入并添加Firebase支持;
2.Firebase添加成功后,谷歌会返回一个 google-services.json 文件;
以上两步都可以联系HeadOffice进行操作:
1、在执行第1步时需要向HeadOffice提供 包名 和 签名证书SHA1(如上图); 注:如果需要用到 谷歌登录 功能,则“签名证书SHA1”字段必须提供。 2、执行完第2步后,需要向HeadOffice获取google-services.json文件(里面包含project_number)和Server API key。 注:project number用于客户端接入FCM,而Server API key用于LCM服务器向google服务器发送push的认证token。 |
3.将 google-services.json 文件放到项目工程的assets目录下(assets一般位于项目根目录)
4.登录LCM OPE工具上传Server API key(上面步骤HO返回的),并配置测试用的远程推送消息;
注意:如果在OPE工具上配置的推送发送失败,如下图:
此时请联系LCM后台管理人员进行排查。
接入FCM时,请务必确认在AndroidManifest.xml中配置了以下内容(这里的com.denachina.pickle需要统一替换成 第1步提供的包名):
<!-- ############################# Google登录 & FCM START ############################# -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- An applicationPackage + ".permission.C2D_MESSAGE" permission to prevent
other Android applications from registering and receiving the Android application's messages.
The permission name must exactly match this pattern—otherwise the Android application will
not receive the messages. -->
<permission android:name="com.denachina.pickle.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.denachina.pickle.permission.C2D_MESSAGE" />
<!-- ############################# Google登录 & FCM END ############################# -->
<!-- ############################# Google商店 START ############################# -->
<!-- GooglePlay service version -->
<!-- Also used by Tapjoy/Facebook -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Google project number, used by Google商店Auth & GCM -->
<!-- googleProjectNumber must be defined use the following format '@string/googleProjectNumber', do not fill its value into the meta-data directly -->
<meta-data android:name="googleProjectNumber" android:value="@string/googleProjectNumber"/>
<!-- Google base64 encoded public key, used by IAB -->
<meta-data android:name="googleBase64EncodedPublicKey" android:value="@string/googleBase64EncodedPublicKey"/>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/lcm_dena_small_icon" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/lcm_notification_accent_color" />
<activity
android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:exported="false"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- ############################# Google商店 END ############################# -->
<!-- ############################# FCM START ############################# -->
<!-- FCM Services -->
<service android:name="com.denachina.lcm.pushmessageprovider.fcm.LCMFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.denachina.lcm.pushmessageprovider.fcm.LCMFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<!-- FCM Receiver -->
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.denachina.pickle" />
</intent-filter>
</receiver>
<!-- FCM Init Provider -->
<provider
android:name="com.denachina.lcm.pushmessageprovider.fcm.LCMFirebaseInitProvider"
android:authorities="com.denachina.pickle.firebaseinitprovider"
android:exported="false"
android:initOrder="100" />
<!-- ############################# FCM END ############################# -->
最后,请在游戏主Activity下添加以下intent-filter:
<!-- Your game activity -->
<activity
android:name="com.denachina.pickle.SampleActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- 下面的3个category缺一不可,否则点击远程推送的图标后无法跳转到APP -->
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.denachina.lcm.category.LAUNCHER" />
</intent-filter>
</activity>
而Project Number (可于google-services.json里查到)需要在res/avlues/strings.xml中配置:
<!-- Google GCM SenderID -->
<string name="googleProjectNumber">74374583821</string>
请务必将googleProjectNumber按以下方式配置在strings.xml文件中:
<meta-data android:name="googleProjectNumber" android:value="@string/googleProjectNumber"/>
由于该值一般为很长的纯数字,若直接将写入meta-data的value属性中,会造成googleProjectNumber取不到正确值(会被取为科学计数法),GCM功能异常;
开发人员请注意:
项目组在出包时如果需要经过LCM打包工具,则需要在配置OPE的打包参数时注意一下操作: 由于打包工具会将Manifest中googleProjectNumber的值直接覆盖为真实OPE工具上指定的真实值,会出现上面提到的取错值的问题。为避免此问题,那么在OPE工具上配置“打包参数”时googleProjectNumber的值务必在开头增加"\0",这样会强制将这串数字读取为string |