我正在使用FCM进行通知。由于某些问题,我在使用FCM Cloud控制台发送消息时没有收到通知。
<service android:name=".MyInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyMessagingService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
这是我的两个班。
public class MyInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyInstanceIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
System.out.println("Refreshed token: " + refreshedToken);
// Util.getInstance().saveDeviceID(UniversalImageLoaderConfigurationApplication.getContext(), refreshedToken);
// sendRegistrationToServer(refreshedToken);
}
}
public class MyMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
// if(LOG)
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Map<String, String> data = remoteMessage.getData();
int id = Integer.parseInt(data.get("id"));
if (id == (Constants.NOTI_NOTIFCATION)) {
String title = data.get("title");
String body = data.get("body");
sendNotification(id, title, body, "");
} else {
sendNotification(1, "", "", "");
}
}
}
我注意到从未公开调用onTokenRefresh()。所以我用了
字符串refreshedToken = FirebaseInstanceId.getInstance()。getToken();
在我的MainActivity中,我正在获取tokenID。 FirebaseInstanceIdService和FirebaseMessagingService可能未运行。下面是我的摇篮
implementation 'com.google.firebase:firebase-messaging:17.0.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
我也用过
apply plugin: 'com.google.gms.google-services'
这是我的根级别
buildscript {
repositories {
// google()
maven { url "https://maven.google.com" }
jcenter()
// mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.1' // google-services plugin
}
}
allprojects {
repositories {
// google()
mavenLocal()
maven { url "https://maven.google.com" }
jcenter()
mavenCentral()
google()
}
}
答案 0 :(得分:0)
操作与您的服务声明相反。更改为此:
<service android:name=".MyInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".MyMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>