不用个推实现本地消息推送

1 篇文章 0 订阅
// 上下文配置
let main = plus.android.runtimeMainActivity();  
let Context = plus.android.importClass('android.content.Context');  
let Notification = plus.android.importClass('android.app.Notification');  
let MyNTF = new Notification.Builder(main, "1");  
let android_R = plus.android.importClass('android.R');  
//通知在Android8.0之后需要创建通知通道,才能弹出来
let NotificationManager = plus.android.importClass("android.app.NotificationManager");
let NotificationChannel = plus.android.importClass("android.app.NotificationChannel");
let channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH)
let nm = main.getSystemService(Context.NOTIFICATION_SERVICE);
nm.createNotificationChannel(channel);
// 通知栏消息配置
MyNTF.setContentTitle('标题'); // 标题
MyNTF.setContentText('内容!'); // 内容
MyNTF.setSmallIcon(android_R.drawable.ic_notification_overlay); // 推送icon
MyNTF.setNumber(10); // 角标数
// 创建通知
nm.notify(1,MyNTF.build());

参考链接1
参考链接2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在iOS应用程序中创建本地消息,你需要做以下几个步骤: 1. 导入UserNotifications框架。 ``` import UserNotifications ``` 2. 请求用户授权发本地通知。在应用程序启动时,你需要请求用户授权。可以在AppDelegate中的didFinishLaunchingWithOptions方法中实现这一点。 ``` UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if let error = error { // 处理错误 } else if granted { // 用户允许通知 } else { // 用户不允许通知 } } ``` 3. 创建一个UNMutableNotificationContent对象,设置通知的标题、正文和声音等。 ``` let content = UNMutableNotificationContent() content.title = "这是标题" content.body = "这是正文" content.sound = UNNotificationSound.default() ``` 4. 创建一个UNNotificationTrigger对象,设置的时间。 ``` let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) ``` 5. 创建一个UNNotificationRequest对象,将UNMutableNotificationContent和UNNotificationTrigger对象传递给它。 ``` let request = UNNotificationRequest(identifier: "local_notification", content: content, trigger: trigger) ``` 6. 将UNNotificationRequest对象添加到UNUserNotificationCenter中。 ``` UNUserNotificationCenter.current().add(request) { error in if let error = error { // 处理错误 } else { // 成功添加通知 } } ``` 这样就可以创建本地消息了。当应用程序在前台运行时,通知会以默认形式显示在设备的通知中心中。如果应用程序在后台运行或未运行,通知将显示在设备的锁屏界面或通知中心中,并在设备上发出声音和震动。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值