android - 如何在 nativescript 中显示抬头通知?
问题描述
我正在尝试在本机脚本角度应用程序中创建抬头通知。我按照以下两个链接中的教程进行操作,并将逻辑复制到本机脚本。它能够正确创建通知,但我没有看到像这样的抬头通知
https://stackoverflow.com/a/50296323。本机脚本代码如下:
public postNotification(title: string, msg: string) {
var context = app.android.context;
let channelId = "12345678";
let channel = new android.app.NotificationChannel(channelId, "Channel title", android.app.NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description")
var manager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
var builder = new androidx.core.app.NotificationCompat.Builder(context, channelId);
builder.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(msg)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setPriority(androidx.core.app.NotificationCompat.PRIORITY_HIGH)
let notificationManagerCompat = (<any>androidx.core.app.NotificationManagerCompat).from(context);
notificationManagerCompat.notify(0, builder.build());
}
https://v4all123.blogspot.com/2018/11/simple-example-of-heads-up-notification.html nativescript 代码:
public sendNotification(title: string, msg: string) {
var activity = app.android.foregroundActivity || app.android.startActivity;
var notifyManager = null
var NOTIFY_ID = 1002
var name = "KotlinApplication"
var id = "kotlin_app"
var description = "kotlin_app_first_channel"
var intent
var pendingIntent
var builder
if (notifyManager == null) {
notifyManager = activity!!.getSystemService(android.content.Context.NOTIFICATION_SERVICE)
}
var importance = android.app.NotificationManager.IMPORTANCE_HIGH
var mChannel = notifyManager.getNotificationChannel(id)
if (mChannel == null) {
mChannel = new android.app.NotificationChannel(id, name, importance)
mChannel.description = description
mChannel.enableVibration(true)
mChannel.lightColor = "GREEN"
mChannel.vibrationPattern = [100, 200, 300, 400, 500, 400, 300, 200, 400]
notifyManager.createNotificationChannel(mChannel)
}
builder = new androidx.core.app.NotificationCompat.Builder(activity!!, id)
intent = new android.content.Intent(activity, (<any>com).tns.NativeScriptActivity.class)
intent.flags = android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP || android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP
pendingIntent = android.app.PendingIntent.getActivity(activity, 0, intent, 0)
builder.setContentTitle("Heads Up Notification") // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(msg) // required
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker("Notification")
.setVibrate([100, 200, 300, 400, 500, 400, 300, 200, 400])
var dismissIntent = new android.content.Intent(activity, (<any>com).tns.NativeScriptActivity.class)
dismissIntent.action = "DISMISS"
dismissIntent.flags = android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP || android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP
var pendingDismissIntent = android.app.PendingIntent.getActivity(activity, 0, dismissIntent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT)
var dismissAction = new androidx.core.app.NotificationCompat.Action(android.R.drawable.ic_baseline_notification_important_24px,
"DISMISS", pendingDismissIntent)
builder.addAction(dismissAction)
var notification = builder.build()
notifyManager.notify(NOTIFY_ID, notification)
}
解决方案
推荐阅读
- html - 在Angular 8中第二次单击“删除所选”按钮时表格未更新
- javascript - 未捕获的类型错误:执行计时器函数时,second_input 为空
- python - 磁盘上的文件更改时自动重新加载 jupter 笔记本
- c# - FluentValidation 规则
- python-3.x - 将字典字典转换为 Pandas 中的逐行数据框
- arrays - 如何将二维数组分配给切片切片?
- c++ - C++ _Tchar 单位 szTestString[] 如何分配字符串值
- vue.js - 从 Vue 访问 CakePHP webroot 目录:图像加载
- android - TextView onclick 方法使 android 应用程序崩溃
- r - 是否有 R 包或函数对数据集进行二次采样以近似某个分布?