Firebase通知Android:onMessageReceived无法在真实设备上工作
最近,我正在使用firebase的消息服务进行android项目,我检查了所有教程,但没有成功。。。我不知道会发生什么。。。这就是我现在得到的 清单文件Firebase通知Android:onMessageReceived无法在真实设备上工作,android,firebase,firebase-cloud-messaging,android-notifications,Android,Firebase,Firebase Cloud Messaging,Android Notifications,最近,我正在使用firebase的消息服务进行android项目,我检查了所有教程,但没有成功。。。我不知道会发生什么。。。这就是我现在得到的 清单文件 <application> ... <service android:name=".MFireBaseMessagingService" android:enabled="true" android:exported="true">
<application>
...
<service
android:name=".MFireBaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>you
</service>
<service
android:name=".MFireBaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
project/build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile files('libs/ksoap2-android-assembly-3.0.0-RC.4-jar-with-dependencies.jar')
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
}
apply plugin: 'com.google.gms.google-services'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
MFIRebaseInstancedService类
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MFireBaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG= "FirebaseInsIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MFireBaseMessagingService extends FirebaseMessagingService
private static final String TAG= "FireBaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size()>0)
{
Log.d(TAG, "Message data: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().toString());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
}
}
MFirebaseMessagingService类
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MFireBaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG= "FirebaseInsIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MFireBaseMessagingService extends FirebaseMessagingService
private static final String TAG= "FireBaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size()>0)
{
Log.d(TAG, "Message data: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().toString());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
}
}
我已将json文件导入应用程序目录。。我会感激你的帮助!谢谢 要获取通知,您需要向服务器提供推送令牌,所以服务器将为此发出通知。如果您想自己测试通知,请转到FCM控制台并从那里触发通知。实际上,我有一个方法来获取令牌并发送到我正在运行的web服务…好,您确定您的服务器正在使用该令牌发送通知吗?只有在“是”的情况下,您才能提供服务器使用推送触发的通知负载和数据。感谢回复Avi,我已经刷新了令牌并在服务器上注册了,很明显令牌即将过期,因为昨天我正在处理通知,当我发送了很多通知时,我刚刚收到一个。。。