当前位置: 代码迷 >> Android >> Android Firebase:当app在后台时,系统中没有显示华为p8推送通知?
  详细解决方案

Android Firebase:当app在后台时,系统中没有显示华为p8推送通知?

热度:162   发布时间:2023-08-04 10:08:05.0

根据 :

当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...."]
}

响应:

{
 "multicast_id": 6105324374411246344,
 "success": 1,
 "failure": 0,
 "canonical_ids": 0,
 "results": [
   {
    "message_id": "0:1486655756341577%dc81f05ff9fd7ecd"
   }
  ]
 }

这个技术在应用程序处于前台时工作,但是在后台时它不起作用

Android代码:

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());
}

表现

  <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>

每当我们在华为手机上遇到问题时,都是因为该应用不是“受保护的应用”。

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

在您的清单文件中尝试此操作。 希望您在不修改系统设置的情况下收到推送通知。

<service android:name="com.jibarath.MyFirebaseMessagingServices"
        android:exported="true"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
        </intent-filter>
</service>

stopwithtask = false做了伎俩。

快乐的编码!!!

尝试使用a发送通知

  to: 

代替

registration_ids

只有当有多个接收者时,文档才会告知我们registration_ids。

registration_ids字符串数组
此参数指定接收多播消息的设备列表(注册令牌或ID)。 它必须包含至少1个且最多1000个注册令牌。仅将此参数用于多播消息传递,而不是单个收件人。 仅使用HTTP JSON格式允许多播消息(发送到多个注册令牌)。

同样如您所述,当设备在后台并且有效负载内部存在通知标记时,将不会调用onMessageReceived。

系统应该能够自己处理它。 确保图标和声音正确保存在适当的文件夹中。

如果一切正常,请检查设备是否处于打盹模式(api> = 23)。 高优先级消息应该从打瞌睡中唤醒设备,但如果设备制造商已经篡改了android os或调整它,这可能不起作用。

如果通知仍未发送,则可能是设备特定的错误。 检查另一台设备进行确认。

如果所有其他方法都失败,请执

我还建议只发送数据有效负载而不是通知和数据。 如果你的目标只是android,那么它可以更好地控制处理什么类型以及何时显示通知。

无论是背景还是前景,数据有效载荷都将始终有效。 它将始终触发onMessageReceived。

  相关解决方案