当应用程序被终止或不在前台时,FCM 通知在某些设备上不起作用



当应用程序被用户杀死(由用户从应用程序托盘中滑开(或被系统操作系统杀死时,是否对使FCM通知在某些设备工作进行了一些研究,但没有找到任何解决方案,如果您可以提供解决方案或告诉我我错了什么,将不胜感激。通知是数据有效负载

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String payload = "",notification_message = "", channelId ="", channelName="AAA", channelDescription="";
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        String title = null, body = null;
        if(remoteMessage.getNotification() != null) {
            Log.d("REMOTE_DATA",remoteMessage.getNotification().getBody());
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        }
        if(remoteMessage.getData() != null) {
            if(remoteMessage.getData().get("title") != null) 
               title = remoteMessage.getData().get("title");
            if(remoteMessage.getData().get("message") != null) 
               body = remoteMessage.getData().get("message");
            if(remoteMessage.getData().get("channelName") != null) 
               channelName = remoteMessage.getData().get("channelName");
        }
    }
    //Setting up Notification channels for android O and above
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Log.e(TAG, "onMessageReceived: => " + android.os.Build.VERSION.SDK_INT);
        if(notificationChannels.containsKey(channelName)) {
            JSONObject channelData = (JSONObject) notificationChannels.get(channelName);
            try {
                channelId = channelData.getString("channelId");
                channelName = channelData.getString("channelName");
                channelDescription = channelData.getString("channelDescription");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
            if (notificationChannel == null)
                setupChannel(channelId, channelName, channelDescription);
        }
    }
    Log.d(TAG, "Notification Message Body: " + title);
    Log.d(TAG, "Notification Message Body: " + body);
    if(title != null && body != null) {
        apptivity_variable.createNotification(channelId,title, body);
        db.insertNotification(title, body, remoteMessage.getData().toString());
    }
}

尝试注册其他服务以在后台识别应用以触发该服务

public class FirebaseBackgroundService extends BroadcastReceiver{
  private static final String TAG = "FirebaseService";
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "I'm in!!!");
    if (intent.getExtras() != null) {
      for (String key : intent.getExtras().keySet()) {
        Object value = intent.getExtras().get(key);
        Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
        if(key.equalsIgnoreCase("gcm.notification.body") && value != null) {
          Bundle bundle = new Bundle();
          Intent backgroundIntent = new Intent(context, BackgroundSyncJobService.class);
          bundle.putString("push_message", value + "");
          backgroundIntent.putExtras(bundle);
          context.startService(backgroundIntent);
        }
      }
    }
  }
}

此外,不要忘记在清单中注册它.xml

<receiver android:exported="true" android:name=".FirebaseBackgroundService" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </receiver>

我遇到了同样的问题,经过搜索和变通,我发现用户需要手动启用自动启动和电池优化权限以获取更多详细信息,请参阅此链接

这个问题通常出现在大多数中国手机中,以优化电池等。

此链接将解决您的问题

FCM 中有两种不同类型的推送消息优先级:普通优先级和高优先级。只有高优先级推送才会在应用程序处于后台时唤醒应用程序,并且是低电耗模式限制的主题。您可以在本文中阅读有关它的更多信息

要提高推送消息的优先级,您必须将此部分添加到推送消息的正文中:

"android":{"priority":"high"}

有关推送消息的更多信息,请参阅此处

相关内容

  • 没有找到相关文章

最新更新