【问题标题】:Android Firebase : Huawei p8 push Notification is not shown in system when app is in background?Android Firebase:当应用程序在后台时,系统中不显示华为 p8 推送通知?
【发布时间】:2017-02-09 16:29:00
【问题描述】:

根据Firebase Documentation

应用在前台时:

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>

【问题讨论】:

  • 你在什么设备上测试?
  • onMessageReceiveived 不会在后台调用,因为您的有效负载具有通知标签。默认的android系统应该自己处理通知。可能是图标或声音的问题……试试吧
  • 代码看起来没问题。你有没有在不同的设备上试过,看看是不是一样的情况?
  • @AL。我试过三星和它的工作,但在华为它不工作

标签: android firebase firebase-cloud-messaging


【解决方案1】:

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

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

【讨论】:

  • 那么 inshort 和 dailyhunt 等其他应用在不更改设置的情况下表现如何?
  • @syedjibharat 你找到答案了吗?
【解决方案2】:

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

<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 成功了。

编码愉快!!!

【讨论】:

    【解决方案3】:

    尝试使用

    发送通知
      to: 
    

    而不是

    registration_ids
    

    文档建议我们仅在有多个接收者时才使用registration_ids。

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

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

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

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

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

    如果所有其他方法都失败,请执行以下操作:

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

    无论是后台还是前台,只有数据负载始终有效。它总是会触发 onMessageReceived。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多