【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
}

代碼配置

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

獲取推送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’ 雖然能打包成功,但是會奔潰。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章