前言:最近在维护一个国内外都在运行的项目,然后客户那边推送出问题了。
因为接收的前任的项目,没搞过。决定从头学习一波。
fcm接入
参考的所有资料:
- 简书某篇博客
- 官方文档
- stackoverflow - 336
fcm官网
导入sdk
项目级(build.gradle)中的配置
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
// ...
// Add the following line:
classpath 'com.google.gms:google-services:4.3.2' // Google Services plugin
}
}
allprojects {
// ...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
// ...
}
}
应用级(build.gradle)中的配置
apply plugin: 'com.android.application'
android {
// ...
}
// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
dependencies {
// ...
// Add the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics:17.2.1'
// Add the SDK for Firebase Cloud Messaging
implementation 'com.google.firebase:firebase-messaging:20.0.0'
// Getting a "Could not find" error? Make sure that you've added
// Google's Maven repository to your root-level build.gradle file
}
代码配置
继承FirebaseMessagingService
public class FcmReceiverService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.i("FcmReceiverService", "onMessageReceived");
}
@Override
public void onNewToken(String token) {
Log.i(TAG, "Refreshed token: " + token);
// 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(token);
}
}
AndroidManifest.xml 中注册
<service
android:name=".FcmReceiverService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- 图标 -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<!-- 背景色 -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
获取推送Token
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.i("isFcm", "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.i("FcmToken", msg);
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.i("FcmMsg", msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
关于 国内集成了 fcm 杀死 app 还是无法收到 推送 的问题
这里引用 delpo 大佬的回答:
- 因为按照安卓系统的逻辑,如果一个 app 被"force stop",那么就不应该被任何行为唤醒,那么自然也就不应该收到通知.
事实上大部份国外应用,按 home 键或者划掉,都不会后台常驻,而是留下缓存进程,这样就能收到 fcm 通知.
所以归根结底还是国内环境给你的错觉.
PS
- google-service.json 文件一定不能忘。
- 国产手机在APP杀死后无法接收。
- 测试推送时每次信息最好不要相同,相同的信息系统会过滤掉。
- 获取到的token就是测试时需要用到的FCM注册令牌。
- 一定不能忘记 apply plugin: ‘com.google.gms.google-services’ 虽然能打包成功,但是会奔溃。