FCM - 重新启动应用程序时未收到通知 [英] FCM - Not Receiving Notifications when the app is relaunched
问题描述
这是我的FireBaseMessagingService类
public class MyFirebaseMessagingService扩展了FirebaseMessagingService {
public static String TAG =Firebase;
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.d(TAG,From:+ remoteMessage.getFrom());
Log.d(TAG,Notification Message Body:+ remoteMessage.getData()。get(message));
notification(remoteMessage.getData()。get(message));
}
void通知(字符串s)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.logo);
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentTitle(App);
mBuilder.setContentText(s);
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this,MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(101,mBuilder.build());
$ p这是我的FirebaseInstance类 public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
public static String TAG =Firebase;
@Override
public void onTokenRefresh(){
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,Refreshed token:+ refreshedToken);
$ b $ / code $
$ b AndroidManifest.xml - < service
android:name =。Helpers.MyFirebaseMessagingService>
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>
< service
android:name =。Helpers.MyFirebaseInstanceIDService>
< intent-filter>
< action android:name =com.google.firebase.INSTANCE_ID_EVENT/>
< / intent-filter>
< / service>
<使用权限android:name =android.permission.AUTHENTICATE_ACCOUNTS/>
< uses-permission android:name =android.permission.BLUETOOTH_ADMIN/>
< uses-permission android:name =android.permission.INTERNET/>
<使用权限android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.WAKE_LOCK/>
< uses-permission android:name =android.permission.SYSTEM_ALERT_WINDOW/>
< uses-permission android:name =android.permission.RECEIVE_BOOT_COMPLETED/>
当应用程序重新启动时,是否有可能获得待定通知?我认为这是可能的,因为你使用的服务,但Android不会让你收到它,直到你添加某种权限。研究什么权限将使这个条件成为可能,并请更新我们。
添加以下权限,那么你应该是好的。
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
<使用权限android:name =android.permission.WAKE_LOCK/>
I migrated to Firebase Cloud Messaging for push notifications. Every thing works great , I get notifications whenever the app is open or running in background , but when the app is closed and a message is sent from the server then you don't receive it in the device ( obviously ). Now when I reopen the app , I should get the pending notification ( the notification sent while the app was closed ) but I don't get the notification.
This is my FireBaseMessagingService class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG="Firebase";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("message"));
notification(remoteMessage.getData().get("message"));
}
void notification(String s)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.logo);
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentTitle("App");
mBuilder.setContentText(s);
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(101,mBuilder.build());
}
}
This is my FirebaseInstance class
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
public static String TAG="Firebase";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
AndroidManifest.xml -
<service
android:name=".Helpers.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".Helpers.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Is it possible to get the Pending Notification when the app is relaunched ?
解决方案 i think it is possible since you're using a service, but android won't let you receive it until you add some sort of permission. Research for what permission will make this condition possible, and please update us.
add the following permission, then u should be good.
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
这篇关于FCM - 重新启动应用程序时未收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!