简体   繁体   English

如何使用 Ionic 2 在 Android 应用程序图标中增加徽章计数通知?

[英]How to increase badge count notification in the Android app icon using Ionic 2?

I developed an app for Android using Ionic 2. The app works fine and the notifications are sended correctly to the app.我使用 Ionic 2 为 Android 开发了一个应用程序。该应用程序运行良好,并且通知已正确发送到该应用程序。 However, always I receive a push notification, the icon badge wich count the notifications does not appear.但是,我总是收到推送通知,但不会出现计数通知的图标徽章。

I tried a lot, I searched a lot in the internet and none solutions works.我尝试了很多,我在互联网上搜索了很多,但没有一个解决方案有效。

Informations:信息:

  1. I have the cordova-plugin-badge installed.我安装了cordova-plugin-badge
  2. In my app.component.ts I already imported the Badge Plugin: import { Badge } from 'ionic-native';在我的app.component.ts我已经导入了徽章插件: import { Badge } from 'ionic-native';
  3. To test, I setted the badge count to 10, with this code: Badge.set(10);为了测试,我将徽章计数设置为 10,使用以下代码: Badge.set(10); - I putted this code inside constructor in app.components.ts . - 我将此代码放在app.components.ts constructor中。

I followed the Ionic tutorial to use the badge.我按照Ionic 教程使用了徽章。

I tried to develop using the plugin tutorial , but it is not especific to Ionic.我尝试使用插件教程进行开发,但它并不是 Ionic 特有的。 Then, I dont know how this can help.然后,我不知道这有什么帮助。

I used the Ionic 2 tutorial to do the push notifications and, how I said, works fine.我使用Ionic 2 教程来做推送通知,而且,我说,工作正常。

Well, the Ionic documentation about the Badge informes that I have to use the Badge.increase(x) .好吧,关于 Badge 的 Ionic 文档通知我必须使用Badge.increase(x) I don't know where to put this code.我不知道把这段代码放在哪里。 I thought I have to specify: When the notifications comes to the device, then Badge.increase(x) .我想我必须指定:当通知到达设备时,然后Badge.increase(x) But, I don't know how to do this.但是,我不知道该怎么做。

My Push notification code is:我的推送通知代码是:

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

But this code runs only with the app opened.但是此代码仅在打开应用程序时运行。 When I receive a background notification, how to increase the badge?当我收到后台通知时,如何增加徽章?

Another important information:另一个重要信息:

I emulate the Android using Android Emulator, provided by Android Studio.我使用 Android Studio 提供的 Android Emulator 模拟 Android。 The device specs . 设备规格

In the Android Device Monitor, I see this error:在 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

The error list continues here .错误列表在此继续

Try to search for this error in Google.尝试在 Google 中搜索此错误。 There are few results.结果很少。 Zero results specifics to Ionic. Ionic 的零结果细节。 Awesome, huh?厉害吧?

And... I see badge count notifications in Android.而且...我在 Android 中看到了徽章计数通知。 Is possible to do this.有可能做到这一点。 But, how?但是,怎么样?

Sorry my english and the long text.对不起我的英语和长文本。

Thanks!谢谢!

EDIT 1 --------------------编辑 1 --------------------

My environment:我的环境:

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

EDIT 2 --------------------编辑 2 -----

My PHP code wich send the notification to Ionic via CURL:我的 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);

you need to send something like this to set badge to 5:你需要发送这样的东西来将徽章设置为 5:

{ "data": {
"badge": "5"},"to" : "your push token"}

notice to make this work for android you should not send notification object with it.请注意,要使这项工作适用于 android,您不应该使用它发送通知对象。 Only data.只有数据。 This way android app will wake up.这样android应用程序就会唤醒。 On IOS you can get current badge and increment it using plugin.在 IOS 上,您可以获取当前徽章并使用插件增加它。 What I usually do it to calculate the badge on the backend and sent new badge我通常做什么来计算后端的徽章并发送新徽章

I just recently ran into the same problem.我最近遇到了同样的问题。 Android does not support badges on push notifications by default.默认情况下,Android 不支持推送通知上的徽章。 But it can be done: To make this work, you need a combination of ionic-native/badge with push notifications.但这是可以做到的:要使这项工作发挥作用,您需要将ionic-native/badge与推送通知结合使用。

Push notifications can be sent as a background notification by setting content_available: 1 in the payload, reference: http://docs.ionic.io/services/push/#ios-background-notifications通过在有效负载中设置content_available: 1 ,可以将推送通知作为后台通知发送,参考: http : //docs.ionic.io/services/push/#ios-background-notifications

Although the link mentions specifically iOS, you can get it to work on Android by using the following payload through Ionic Push notifications:尽管该链接特别提到了 iOS,但您可以通过 Ionic 推送通知使用以下有效负载,使其在 Android 上运行:

"notification": {
    "android": {
        "content_available": 1,
        "payload": {
            "badge": 5,
            "title": "Alert",
            "text": "Notifications"
        },
        "data": {
            "badge": 5
        }
    }
}

What this means:这是什么意思:

content_available:1 tells the device when receiving the push notification that it's a background notification that should not alert the user. content_available:1在收到推送通知时告诉设备这是一个不应提醒用户的后台通知。 So your app will be able to process the notification without bothering the user.因此,您的应用程序将能够在不打扰用户的情况下处理通知。

payload This is a preference, but here's why I have this: there's a magic combination, for me, with these background push notifications. payload这是一个偏好,但这就是我为什么要这样做的原因:对我来说,这些后台推送通知有一个神奇的组合。 I don't want the user alerted when the app is closed, but I want to handle it when the app is open.我不希望用户在应用程序关闭时收到警报,但我想在应用程序打开时处理它。 You can't put a title and message on a background push notification and still have it silently delivered in the background (at least on Android), so I put this information on the payload and then manually access it.你不能在后台推送通知上放置titlemessage ,并且仍然让它在后台静默传递(至少在 Android 上),所以我把这些信息放在有效负载上,然后手动访问它。

data This is the magic sauce that allows Android to process this background notification. data这是让 Android 处理这个后台通知的魔法。 Using the push plugin ionic provides, the payload structure is extremely pertinent to how the app processes the notification.使用 ionic 提供的推送插件,负载结构与应用程序处理通知的方式极为相关。 Here's a good reference: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads这是一个很好的参考: https : //github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads

Now, to handling the message in the app.现在,处理应用程序中的消息。

First, make sure you've properly followed the ionic-native/push documentation for setting up the plugin and injecting it into your app.首先,请确保您已正确遵循ionic-native/push文档来设置插件并将其注入您的应用程序。

Define the provider in app.moduleapp.module定义提供者

import { Badge } from '@ionic-native/badge';

providers: [
    Badge
]

Import all the things you'll need:导入所有你需要的东西:

import { Push, PushToken, IPushMessage } from '@ionic/cloud-angular';
import { Platform, Events } from 'ionic-angular';
import { Badge } from '@ionic-native/badge';

Inject it:注入它:

constructor(
    private badge: Badge
) {}

Use it to handle a payload:使用它来处理有效载荷:

this.push.rx.notification()
    .subscribe((message: IPushMessage) => {
        console.log(message);
        if (message.title && message.text) {
            let notification: AppNotification = {
                title: message.title,
                message: message.text
            };
            // Handle the push notification in app.
        }

        let payload: any = message.payload;
        // Get the badge number off the payload.
        if(payload && payload.badge) {
            // Using the badge plugin, set the badge number.
            this.badge.set(Number(payload.badge));
        }

        if(payload && payload.title && payload.text) {
            let notification: AppNotification = {
                title: payload.title,
                message: payload.text
            };
            // Do something with the background notification if the app is open.
        }
    });

Following the ionic documentation for the badge plugin, when you receive a push notification, you can take the payload data and then use it to set the badge number.按照badge插件的离子文档,当您收到推送通知时,您可以获取有效负载数据,然后使用它来设置徽章编号。 Additionally, if you would like you can also handle the push notification in the app.此外,如果您愿意,您还可以在应用程序中处理推送通知。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android App Icon中的通知计数徽章 - Notification Count Badge in Android App Icon 如何在后台在Android应用程序图标上计算推送通知消息徽章 - How to count Push notification message badge on android app icon in background 如何以编程方式在Android上的应用程序图标上显示推送通知徽章计数? - How to show push notification badge count on app icon in android programmatically? 取消推送通知并打开应用程序时如何增加计数徽章? - How to increase the count badge when push notification recevied and app is open? 如何在三星设备中的应用程序图标徽章上推送通知消息计数 - how to push notification message count on app icon badge in samsung devices 如何在奥利奥的应用程序图标上显示多个通知计数徽章? - How to display multiple notification count badge on app icon in Oreo? 在android中的app图标上添加通知徽章 - adding notification badge on app icon in android Android:如何为图标创建通知徽章? - Android: how to create a notification badge for an icon? 在没有推送通知的情况下更新应用程序图标上的徽章计数 - Update badge count on app icon without push notification 我们如何在Android中以编程方式为“锁定屏幕通知和徽章图标”等应用启用通知设置 - How can we enable notification setting in android for app like “lock screen notification and badge icon” programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM