如何设置Android应用程序以使用FCM?

时间:2017-04-20 07:35:06

标签: android firebase push-notification firebase-cloud-messaging

我已经在教程中一步一步地完成了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类我没有给任何logcat,任何想法?

更新 到目前为止,我还没有在收到消息时找到解决方案。扩展FirebaseMessagingService的类仍然无效,在logcat中没有显示任何内容,但它确实显示如下内容:D / FA:记录事件(FE):_ nf,Bundle [{_ o = fcm,_ndt = 0,_nmn = Bayu测试PN,_nmt = 1492745322,_nmid = 2820998932030178856}]

2 个答案:

答案 0 :(得分:1)

我现在有工作,问题是因为我把服务放在应用程序之外,所以服务必须在应用程序内部,所以onReceivedMessage会起作用

答案 1 :(得分:0)

你可能跳过了这一步,因为我没有看到你提到它:
在您的模块Gradle文件(通常是app / build.gradle)中,在文件底部添加apply plugin行以启用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'