如何在nativescript中显示抬头通知?

时间:2019-09-22 12:55:44

标签: android nativescript angular2-nativescript nativescript-angular

我正在尝试在本机脚本angular应用程序中创建Heads up通知。我按照下面两个链接中的教程进行操作,并将逻辑复制到本机脚本。它能够正确创建通知,但是我发现heads up notification like this

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本机脚本代码:

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

0 个答案:

没有答案