首页 > 解决方案 > 如何在 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)
}

标签: androidnativescriptangular2-nativescriptnativescript-angular

解决方案


推荐阅读