Chrome浏览器使用Notification通知消息推送

代码如下

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      function createNotify(title, options) {
        var PERMISSON_GRANTED = "granted";
        var PERMISSON_DENIED = "denied";
        var PERMISSON_DEFAULT = "default";
        
        // 如果用户已经允许,直接显示消息,如果不允许则提示用户授权
        if (Notification.permission === PERMISSON_GRANTED) {
          notify(title, options);
        } else {
          Notification.requestPermission(function (res) {
            if (res === PERMISSON_GRANTED) {
              notify(title, options);
            }
          });
        }

        // 显示提示消息
        function notify($title, $options) {
          var notification = new Notification($title, $options);
          console.log(notification);
          notification.onshow = function (event) {
            console.log("show : ", event);
          };
          notification.onclose = function (event) {
            console.log("close : ", event);
          };
          notification.onclick = function (event) {
            console.log("click : ", event);
            // 当点击事件触发,打开指定的url
            window.open(event.target.data)
            notification.close();
          };
        }
      }

      createNotify("新的消息", {
        body: "你有一个奖品待领取",
        icon: "https://www.baidu.com/favicon.ico",
        data: "https://www.baidu.com/"
      });

      /* 依次打印
       * show:   Event Object(事件对象),事件的type为"show"
       * click:  Event Object(事件对象),事件的type为"click"。点击消息后消息被关闭,跳到close事件。
       * close:  Event Object(事件对象),事件的type为"close"
       */
    </script>
  </body>
</html>

各浏览器的支持不是很统一
https://www.caniuse.com/?search=Notification
在这里插入图片描述

文件直接打开没有效果,需要由后台服务提供页面

参考
Notification 浏览器的消息推送

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android通知栏是用来显示推送消息的重要组件之一。要在Android应用中实现通知推送消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发送通知:通过NotificationManager类的notify()方法发送通知。指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发送一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值