Ios@IONIChybrid APP(ionic)Ionic之路

Ionic3 本地消息推送

2017-11-06  本文已影响310人  spilledyear

项目上有一个消息推送的功能,一开始想使用极光推送,在安卓上测试比较顺利,但是IOS上需要什么证书,没有开发者账号,感觉好麻烦。后面就想在每次启动app的时候,本地推送一次消息,碰巧官网上发现了这个插件,de.appplant.cordova.plugin.local-notification。


image.png

安装插件

ionic cordova plugin add de.appplant.cordova.plugin.local-notification
npm install --save @ionic-native/local-notifications

安装之后,在app.module.ts中引入provider

import { LocalNotifications } from '@ionic-native/local-notifications';
image.png

使用说明

image.png
  testStatus() {
    this.localNotifications.schedule({
      id: 1,
      title: '筑美通知',
      text: '这是显示通知栏的内容',
      icon: 'http://example.com/icon.png',
      at: new Date(new Date().getTime() + 3000),
    });

    this.localNotifications.on('click', (notification) => {
      alert(JSON.stringify(notification));
    });
  }

//这里选择在应用启动的时候调用发送一条消息,也就是 调用 this.testStatus()方法。 
//如上所示,每条消息可以看成是一个对象,text是内容,title是标题,at表示在通知栏上显示的时间。
this.localNotifications.on('click', (notification) => {
  alert(JSON.stringify(notification));
});
//这部分代码表示在通知栏上点击该通知时对应的回调函数,可以此函数中写逻辑功能。

测试结果如下图所示:


点击消息后的效果


上一篇下一篇

猜你喜欢

热点阅读