Android Firebase:当应用程序在后台时,系统中未显示华为 p8 推送通知?

Min*_*rid 2 android firebase firebase-cloud-messaging

根据Firebase 文档

当 App 在前台时:

onMessageReceived 被调用,我可以显示推送通知并对数据进行任何处理(在这种情况下我没问题)。

当应用程序在后台时:

通知系统托盘将显示通知,一旦用户点击通知,启动器活动将被调用,通知数据将在捆绑包中传递给启动器活动。

我的问题是,当应用程序在后台时,通知系统没有显示通知。

我没有使用 Firebase 控制台,因为我无法向通知负载添加数据。所以我使用了 RESTful 客户端 API,但它仍然是同样的问题。

要求:

URL:https://fcm.googleapis.com/fcm/send
Method type: POST
Content-Type: application/json
Authorization : key=AAAAFi0N5KM....
Body:

    {
     "priority" : "high",
     "notification" : {
         "body" : "This week’s edition is now available.",
         "title" : "NewsMagazine.com",
         "icon" : "new",
         "click_action": "OPEN_ACTIVITY_1" ,
         "sound" : "mySound"

     },
     "data": {
         "title": "title " ,
         "body": "any vaslue " 
     },
     "registration_ids": ["fVNdKJsWtNQ:APA91bEJ...."]
}
Run Code Online (Sandbox Code Playgroud)

回复:

{
 "multicast_id": 6105324374411246344,
 "success": 1,
 "failure": 0,
 "canonical_ids": 0,
 "results": [
   {
    "message_id": "0:1486655756341577%dc81f05ff9fd7ecd"
   }
  ]
 }
Run Code Online (Sandbox Code Playgroud)

当应用程序在前台时此技术有效,但在后台时它不起作用

安卓代码:

public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}

private void sendNotification(String messageTitle,String messageBody) {
    Intent intent = new Intent(this, SplashActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    long[] pattern = {500,500,500,500,500};

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.roshetta_logo_home)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)

显现

  <service android:name=".Util.PushNotification.MyFirebaseMessagingService"
         >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".Util.PushNotification.MyFirebaseIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
Run Code Online (Sandbox Code Playgroud)

Fed*_*cci 5

每次我们在华为手机上出现问题时,都是因为该应用程序不是“受保护的应用程序”。

  1. 进入设置
  2. 向下滚动并点击高级设置
  3. 点击电池管理器
  4. 找到受保护的应用程序并选择它
  5. 找到您的应用,并将其设置为受保护