android-Firebase通知推送通知不工作
更新 不幸的是,我没有解决这个问题,我所做的是创建一个新项目。幸运的是,我的新项目成功了。我注意到一件事 从我新创建的项目中,当我发送通知时 消息,一些消息没有到达我的设备。所以我想这是我的 互联网连接问题(仅限我的想法) 我正在尝试使用Firebase实现推送通知的基本接收。我基于本教程: 我的问题是我根本没有收到任何消息。我已经使用Firebase控制台发送了5条以上的消息,但仍然没有发生任何事情 以下是我对FirebaseInstancedService的实现:android-Firebase通知推送通知不工作,android,firebase,google-cloud-messaging,firebase-cloud-messaging,Android,Firebase,Google Cloud Messaging,Firebase Cloud Messaging,更新 不幸的是,我没有解决这个问题,我所做的是创建一个新项目。幸运的是,我的新项目成功了。我注意到一件事 从我新创建的项目中,当我发送通知时 消息,一些消息没有到达我的设备。所以我想这是我的 互联网连接问题(仅限我的想法) 我正在尝试使用Firebase实现推送通知的基本接收。我基于本教程: 我的问题是我根本没有收到任何消息。我已经使用Firebase控制台发送了5条以上的消息,但仍然没有发生任何事情 以下是我对FirebaseInstancedService的实现: public class
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
public static final String debugTag = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token in logcat
Log.e(debugTag, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
My FirebaseMessagingService实现:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String debugTag = "MyFirebaseApp";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(debugTag, "Notification Received!");
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
我的清单文件:
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".messagingservice.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".tokenservice.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
我做错了什么?或者我缺少什么来实施
这件事我真的需要一些帮助。任何帮助都将不胜感激。多谢各位 别忘了,我也遇到了同样的问题,通过定义enabled,在我的服务中导出为true,尝试像我一样做
<service android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</aplication>
然后设置权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
别忘了,我也遇到了同样的问题,通过定义enabled,在我的服务中导出为true,尝试像我一样做
<service android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</aplication>
然后设置权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
在我的情况下,一切正常,但应用程序的通知通道处于非活动状态。因此,请确保您的通知通道在设置中处于活动状态
可能对某些人有所帮助。在我的情况下,一切正常,但应用程序的通知通道处于非活动状态。因此,请确保您的通知通道在设置中处于活动状态 可能会有帮助。
onRefreshToken()
已弃用,使用onNewToken(字符串s)
onRefreshToken()
已弃用,使用onNewToken(字符串s)
您是否收到移动设备系统托盘中的通知?或者只是没有调用
onMessageReceived
方法?您是如何尝试的?我的意思是当应用程序处于前台或后台时?@Chris Palma您是否在模块级目录中包含了google-services.json文件?是的,我在我的目录中包含了json文件。我还尝试将我的应用程序设置为前台和后台。我真的没有收到任何通知。@ChrisPalma在您的代码中,onMessageReceived方法Log.d(debugTag,“收到通知!”)中有下面一行代码>是否在logcat中打印消息?您是否至少在移动设备的系统托盘中收到通知?您是否在移动设备的系统托盘中收到通知?或者只是没有调用onMessageReceived
方法?您是如何尝试的?我的意思是当应用程序处于前台或后台时?@Chris Palma您是否在模块级目录中包含了google-services.json文件?是的,我在我的目录中包含了json文件。我还尝试将我的应用程序设置为前台和后台。我真的没有收到任何通知。@ChrisPalma在您的代码中,onMessageReceived方法Log.d(debugTag,“收到通知!”)中有下面一行代码>是否在logcat中打印消息?您是否至少在移动设备的系统托盘中收到通知?