【发布时间】:2017-04-28 04:12:37
【问题描述】:
任务:在 Android 应用的启动图标上通过推送通知显示通过的徽章计数。
设置:Nativescript + Angular2、Firebase
(您无需在 iOS 上执行任何操作。)
【问题讨论】:
标签: android push-notification angular2-nativescript
任务:在 Android 应用的启动图标上通过推送通知显示通过的徽章计数。
设置:Nativescript + Angular2、Firebase
(您无需在 iOS 上执行任何操作。)
【问题讨论】:
标签: android push-notification angular2-nativescript
注意:只能显示特定品牌的徽章数量。见[1]。您很可能需要这样的手机进行测试(它在我的三星手机上工作)。
我已经安装并设置了插件nativescript-plugin-firebase(例如,该应用可以接收推送通知,并且徽章计数显示在 iOS 上)。
将Android库ShortcutBadger[2]添加到App_Resources/Android/app.gradle中的依赖中:
dependencies {
compile 'me.leolin:ShortcutBadger:1.1.16@aar'
}
创建广播接收器:
android.support.v4.content.WakefulBroadcastReceiver.extend("<your_package>.BadgeBroadcastReceiver", {
onReceive: function (context: any, intent: any) {
if (!isAndroid || intent.getExtras() == null || !intent.hasExtra("gcm.notification.badge")) {
return;
}
let badge = parseInt(intent.getStringExtra("gcm.notification.badge"));
me.leolin.shortcutbadger.ShortcutBadger.applyCount(context, badge);
}
});
将以下行添加到标记<application>中的文件AndroidManifest.xml:
<receiver android:name="<your_package>.BadgeBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</receiver>
为了重置徽章计数,我在包装应用程序的组件(例如 app.component.ts)中添加了对 resume 事件的回调:
constructor(private authService: AuthService, private notificationService: NotificationService) {
application.on(application.resumeEvent, () => this.resetUnreadMessageCount());
}
/**
* Reset the counter of unread messages (in iOS terms: badge count).
*/
resetUnreadMessageCount() {
if (isAndroid) {
me.leolin.shortcutbadger.ShortcutBadger.applyCount(application.android.context, 0);
}
}
[1]How to add a notification badge/count to application icon on Sony Xperia devices?
【讨论】:
目前还没有永久的解决方案,作为一种解决方法,您可以使用两个通知资源图标并根据布尔变量更改模板上的图像。
<TabStripItem>
<Image [src]="notificationIconWithoutAlert ? 'res://bell_solid' : 'res://bell_solid_alert'" class="fas"></Image>
</TabStripItem>
【讨论】: