firebase推送功能(可選)
- 接入firebase推送功能,你需要在
strings.xml上做相應的設置,以及在manifest.xml文件<manifest>跟節點添加相應的權限以及在<application>節點中添加相應的組件聲明(此部分內容在給出的sdk中已配置完成),如下。
string.xml文件配置
- 在strings.xml文件中開啟firebase推送功能:
xml
<!--是否接入firebase推送功能 1:接入,0:未接入(默认)-->
<string name="is_access_firebase">1</string>manifest.xml文件
- 添加權限聲明,需要注意的是,以下權限你只需要在sdk中或遊戲中添加一次即可,否則運行會報錯:
xml
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="{包名}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="{包名}.permission.C2D_MESSAGE" />- 相關組件聲明:
xml
<service
android:name="com.gd.fcmpush.GDFirebaseMessagingService"
android:exported="false" >
<intent-filter android:priority="-500" >
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>- 如果遊戲也有firebase推送功能的話,遊戲只需要在manifest中註冊action為com.gd.fcm.action的廣播,並在廣播的onReceive方法的intent參數中獲取RemoteMessage對象。如下:
- 註冊廣播:
xml
<receiver android:name="{你的自定義廣播接收器}">
<intent-filter>
<action android:name="com.gd.fcm.action" />
</intent-filter>
</receiver>- FcmReceiver類:
java
public class "{你的自定義廣播接收器}" extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
RemoteMessage remoteMessage = (RemoteMessage) intent.getExtras().get("remoteMessage");
// TODO 游戏处理自己接收到firebase推送后的逻辑
}
}