【问题标题】:Firebase doesn't show notifications when app not running应用未运行时 Firebase 不显示通知
【发布时间】:2017-06-14 20:37:36
【问题描述】:

快速提问

当我的应用根本没有运行(但已安装)时,我是否能够接收到默认 android 的 Notifications 菜单(当您向下滑动状态栏时出现)的通知。如果是 - 我该怎么办?

详细说明

我已经在我的 Android 应用中实现了 Firebase 通知。方法如下:

Manifest.xml

<service
    android:name=".utils.FireBase">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
<service
    android:name=".utils.FireBaseService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@mipmap/ic_launcher" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

FireBaseService.java

public class FireBaseService extends FirebaseMessagingService {
    private static final String TAG = FireBaseService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("message")); 
    }
}

FireBase.java

public class FireBase extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        // I used this token in rest.
        Log.e("FirebaseToken", FirebaseInstanceId.getInstance().getToken());
    }
}

好的,现在我要发送通知:

curl -X POST --data "$DATA" https://fcm.googleapis.com/fcm/send --header "Content-Type:application/json" --header "Authorization:key=$KEY"

$DATA

{
  "notification":{ 
      "body": "My notification!"
  },
  "to": "KEY_FROM:FirebaseInstanceId.getInstance().getToken()"
}

回应

{
  "multicast_id": 7830342176959972827,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1485706467606270%8a47512d8a475125"
    }
  ]
}

现在,当我的应用程序正在运行时,我能够接收通知。当我的应用程序进入后台时 - 通知会发送到 Android notifications。现在我关闭我的应用程序(它既不在前台也不在后台)(我的意思是关闭 - 按SQUARE 硬件按钮 -> 向左滑动应用程序,所以我的应用程序不再在任务管理器中)。我想收到来自 firebase 的通知到 Android notifications 菜单。但是当我调用休息服务时没有任何反应。我不关心布局,所以我只想通知用户。

我尝试了使用不同设备的通知:

  1. 真正的安卓设备:基于安卓5.1的小米mi4c。
  2. 安装了基于 android 7.0 + playmarket 的 Genymotion 模拟器。
  3. 安装了基于 andoriod 4.4 + playmarket 的 Genymotion 模拟器。

通知根本不显示。我观察了控制台,我什至没有找到关于它们的日志。我阅读了很多 stackoverflow 帖子,据我所知,我应该能够这样做。

如何归档此目标?如果我无法通过 firebase 接收通知 - 也许我应该改用 GCM

最好的问候,

【问题讨论】:

标签: android firebase push-notification


【解决方案1】:

实际上,关于这个话题的讨论很活跃,因为似乎这种行为会根据制造商执行的 Android 操作系统定制而改变。

这里有一个 Firebase 成员在 GitHub comment 上的回答,证明尚未找到 Firebase 通知的通用解决方案。

所以如果它不能正常工作不应该是你的错(假设你没有在某处犯过错误);

该线程中更有用的 cmets: Comment 1, Comment 2

【讨论】:

  • 嗯,多么好奇……我没有看到任何使用推送通知的案例。如果在应用程序未运行时它不工作,我可以使用 websockets 对我的服务器实现相同的功能......根据stackoverflow.com/a/24314088/3872976 这个答案 GCM 支持该功能。我认为 Firebase 是基于 GCM API 构建的。
  • 我不确定链接的解决方案是否可行。因为推送通知阻止系统最近已添加到自定义操作系统中(您链接的解决方案日期为 2014 年)。也许一个选项可以是一个 SyncAdapter,它会定期检查新的推送通知,在后台你可以启动一些东西来唤醒你的应用程序并开始监听新的通知。顺便说一句,这只是一个想法,我从未尝试过
  • 但是whatssapp,facebook通知甚至没有在最近
  • 有解决办法了吗?
  • 您可以创建BackgroundService 并使用您的应用程序或从BroadcastReceiver + BOOT_COMPLETED 启动它。因此,您的服务将始终运行并能够接收 FCM
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多