我第一次使用FCM。在我的第一个实现中,我使用了以下approach。我收到的通知很完美。 现在,我已经更新了我的应用程序,并尝试使用以下更新的approach。 但是我面临的问题是,当我从通知控制台发送通知时,仍在使用以前版本的通知。
我已将Gradle更新为
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile('com.crashlytics.sdk.android:crashlytics:2.9.5@aar') {
transitive = true;
}
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:design:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:cardview-v7:26.+'
compile 'com.android.support:support-v4:26.+'
compile 'com.google.firebase:firebase-core:16.0.4'
compile 'com.google.android.gms:play-services-ads:15.0.1'
compile 'com.google.firebase:firebase-messaging:17.3.3'
testCompile 'junit:junit:4.12' } apply plugin: 'com.google.gms.google-services'
}
第二个问题是当我在MainActivity.java的oncreate方法中使用此方法来检索新令牌时,java getToken()和getActivity()变成红色。
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, instanceIdResult -> {
String newToken = instanceIdResult.getToken();
Log.e("newToken", newToken);
getActivity().getPreferences(Context.MODE_PRIVATE).edit().putString("fb", newToken).apply();
});
Log.d("newToken", getActivity().getPreferences(Context.MODE_PRIVATE).getString("fb", "empty :("));
我无法解决问题。请帮忙!
这是我的服务
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());
}
public void onNewToken(String token) {
Log.d(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);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}