FCM - 重新启动应用程序时未收到通知 [英] FCM - Not Receiving Notifications when the app is relaunched

查看:355
本文介绍了FCM - 重新启动应用程序时未收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已迁移到Firebase云消息传递以进行推送通知。每件事情都很好,每当应用程序打开或在后台运行时,都会收到通知,但是当应用程序关闭并且从服务器发送消息时,您不会在设备中收到它(显然)。现在,当我重新打开应用程序,我应该得到待处理的通知(应用程序关闭时发送的通知),但我没有得到通知。

这是我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆