【发布时间】:2017-01-26 00:49:27
【问题描述】:
我使用 Ionic 2 为 Android 开发了一个应用程序。 该应用程序运行良好,并且通知已正确发送到该应用程序。 但是,我总是会收到推送通知,但不会出现计数通知的图标徽章。
我尝试了很多,我在互联网上搜索了很多,但没有一个解决方案有效。
信息:
- 我安装了
cordova-plugin-badge。 - 在我的
app.component.ts我已经导入了徽章插件:import { Badge } from 'ionic-native'; - 为了测试,我将徽章计数设置为 10,使用以下代码:
Badge.set(10);- 我将此代码放入constructor中的app.components.ts中。
我跟随Ionic tutorial使用徽章。
我尝试使用plugin tutorial 进行开发,但它不是 Ionic 特有的。然后,我不知道这有什么帮助。
我使用了Ionic 2 tutorial to do the push notifications,正如我所说的,效果很好。
好吧,关于徽章的 Ionic 文档告诉我必须使用 Badge.increase(x)。我不知道把这段代码放在哪里。我想我必须指定:当通知到达设备时,然后Badge.increase(x)。但是,我不知道该怎么做。
我的推送通知代码是:
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
Badge.clear();
});
但此代码仅在应用打开的情况下运行。 当我收到后台通知时,如何增加徽章?
另一个重要信息:
我使用 Android Studio 提供的 Android Emulator 模拟 Android。 device specs。
在 Android 设备监视器中,我看到此错误:
01-25 21:26:54.558: W/PackageManager(6769): Unknown permission com.sec.android.provider.badge.permission.READ in package com.ionicframework.myapp343731
01-25 21:26:54.558: W/PackageManager(6769): Unknown permission com.sec.android.provider.badge.permission.WRITE in package com.ionicframework.myapp343731
错误列表continues here。
尝试在 Google 中搜索此错误。结果很少。 Ionic 的零结果细节。厉害吧?
而且...我在 Android 中看到徽章计数通知。 有可能做到这一点。但是,怎么做?
对不起,我的英文和长文本。
谢谢!
编辑 1 --------
我的环境:
OS: Ubuntu 16.04.1 LTS
node -v: v5.12.0
npm -v: 3.8.6
ionic -v: 2.2.1
cordova -v: 6.4.0
cordova-plugin-badge version: 0.7.4
编辑 2 --------
我的 PHP 代码通过 CURL 向 Ionic 发送通知:
$yourApiSecret = "myApiSecret"; // Available in Ionic Dashboard
$androidAppId = "myAndroidAppId"; // Available in Ionic Dashboard
$api_token = "my.api.token"; // Created in Ionic Dashboard
$data = array(
"tokens" => "send_to_all",
"profile" => "my_created_profile", //Created in Ionic Dashboard
"send_to_all" => true,
"notification" => array(
'title' => $title,
'message' => $message,
)
);
$data_string = json_encode($data);
$ch = curl_init('https://api.ionic.io/push/notifications');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'X-Ionic-Application-Id: '.$androidAppId,
'Content-Length: ' . strlen($data_string),
'Authorization: Bearer '.$api_token
)
);
$result = curl_exec($ch);
【问题讨论】:
-
从后端发送推送通知时,您可以提供徽章计数。
-
嗨@JoeriShoeby,感谢您的回答。但是,我该怎么做呢?我尝试了很多,但没有任何工作。使用 Ionic 2,如何在收到推送通知时在应用图标中添加徽章计数?
-
你有什么后端服务器?
-
@JoeriShoeby 我将 Ionic 应用程序与 Firebase 集成。在我的主机中,使用 PHP,我通过 CURL 向 Ionic 的通知 URL 发送一个 POST。 Ionic 连接到 Firebase 以发送通知。在我遵循的教程下方:Implementing Notification / Sending CURL to Ionic - 只有 CURL 部分 / Ionic's CURL params - 推送通知工作正常
-
请提供您的 PHP 代码。
标签: android cordova ionic-framework notifications ionic2