【发布时间】: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 菜单。但是当我调用休息服务时没有任何反应。我不关心布局,所以我只想通知用户。
我尝试了使用不同设备的通知:
- 真正的安卓设备:基于安卓5.1的小米mi4c。
- 安装了基于 android 7.0 + playmarket 的 Genymotion 模拟器。
- 安装了基于 andoriod 4.4 + playmarket 的 Genymotion 模拟器。
通知根本不显示。我观察了控制台,我什至没有找到关于它们的日志。我阅读了很多 stackoverflow 帖子,据我所知,我应该能够这样做。
如何归档此目标?如果我无法通过 firebase 接收通知 - 也许我应该改用 GCM?
最好的问候,
【问题讨论】:
-
你可以阅读这篇文章,它可能对其他有同样问题的人也有帮助 [link] (medium.com/@shayan.ta69/…)
标签: android firebase push-notification