【Google推送】FCM集成,测试,坑,全记录。

前言:最近在维护一个国内外都在运行的项目,然后客户那边推送出问题了。
因为接收的前任的项目,没搞过。决定从头学习一波。

fcm接入

参考的所有资料:

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
}

代码配置

Android端如何接收

继承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

  1. google-service.json 文件一定不能忘。
  2. 国产手机在APP杀死后无法接收。
  3. 测试推送时每次信息最好不要相同,相同的信息系统会过滤掉。
  4. 获取到的token就是测试时需要用到的FCM注册令牌。
  5. 一定不能忘记 apply plugin: ‘com.google.gms.google-services’ 虽然能打包成功,但是会奔溃。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值