簡體   English   中英

如何設置Android應用程序以使用FCM?

[英]How to setup Android application to use FCM?

我已經完成了本教程中的分步操作,獲取json,添加了dependecies (classpath 'com.google.gms:google-services:3.0.0')
apply plugin (apply plugin: 'com.google.gms.google-services') ,修改清單文件。 不要忘記將它們放到應用程序中,而不是放到它外面!

<service android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<service android:name=".FirebaseIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

創建firebaseMessagingService類(MyFirebaseMessagingService.java)

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}

創建擴展FirebaseInstanceIdService的FirebaseIDService.java

public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer() {
    // Add custom implementation, as needed.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}

這是我的主要活動代碼段:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //FCM
    FirebaseIDService firebaseIDService = new FirebaseIDService();
    MyFirebaseMessagingService myFirebaseMessagingService = new MyFirebaseMessagingService();

然后,我從控制台發送了推送通知,但沒有任何反應。 缺少什么部分? 還是什么不起作用? 我從我創建的兩個類中得到任何logcat信息

更新:這是我為解決Firebase未鏈接問題所做的工作:我需要手動調用該類並獲取令牌,並且它的工作原理

FirebaseIDService firebaseIDService = new FirebaseIDService();
    firebaseIDService.getId();

但是不知何故,推送通知仍然沒有到來,我以前使用的FCM類沒有給出任何日志記錄,有什么想法嗎?

更新:到目前為止,我在收到消息時仍未找到解決方案。 擴展FirebaseMessagingService的類仍無法正常工作,在logcat中未顯示任何內容,但確實顯示了以下內容:D / FA:記錄事件(FE):_nf,Bundle [{__ = fcm,_ndt = 0,_nmn = Bayu測試PN,_nmt = 1492745322,_nmid = 2820998932030178856}]]

我現在可以正常工作了,問題是因為我將服務放在了應用程序之外,所以服務必須在應用程序內,所以onReceivedMessage可以工作

您可能跳過了這一步,因為我沒有看到您提到它:
在您的模塊Gradle文件(通常是app / build.gradle)中,在文件底部添加Apply插件行以啟用Gradle插件:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:10.2.1'

  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

請注意:編譯'com.google.firebase:firebase-core:10.2.1'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM