繁体   English   中英

在Ionic 2中打开应用程序时处理推送通知

[英]Handling Push Notifications when app is open in Ionic 2

在Ionic 2中,我试图处理用户当前在应用程序中时的事件,以便我可以弹出警报消息并导航到其他视图。 在我的App构造函数中,我有以下代码:

export class MyApp {
  private rootPage: any;

  constructor(private platform: Platform, private app: App, private push: Push) {
    if (UserService.isLoggedIn()) { this.rootPage = PickupPage; }
    else { this.rootPage = LoginPage; }

    console.log('We are here! We are here! We are here!');
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      console.log('Registering notifications handler');
      this.push.rx.notification().subscribe(msg => {
        alert(msg.title);
        console.log('Got something!');
        console.dir(msg);
      });
      StatusBar.styleDefault();
    });
  }
}

当我发送通知时,在Android上,我会在Android的下拉栏中看到通知,但是在应用程序内没有控制台或警报,而在iOS上,我什么也收不到。 通知中心中没有控制台消息或警报,也没有通知。

§ ionic -v
2.0.0-beta.37

它看起来像我不得不添加的senderIDCloudSettingspush.pluginConfig.android.senderID工作,就像如下:

const cloudSettings: CloudSettings = {
  'core': { 'app_id': '***' },
  'push': {
    'sender_id': '***',
    'pluginConfig': {
      'ios': { 'badge': true, 'sound': true },
      'android': { 'senderID': '***', 'iconColor': '#d1b561' }
    }
  }
}

不知道这是否是100%,因为安装应用程序后似乎不希望更改CloudSettings对象。

编辑

看来我已经将事情做得更远了。 根据文档,您应在每次打开应用程序时调用this.push.register() 我发现通过将app.ts我的App的构造函数更新为以下内容,推送通知看起来更加稳定:

platform.ready().then(() => {
  // Register to receive push notifications
  this.push.register().then((token: PushToken) => {
    // Send the token to Ionic cloud so we can send to it through the API
    return this.push.saveToken(token)
  });
  // Setup our handler so we can perform actions on push notifications
  this.push.rx.notification().subscribe(msg => {
    console.log(`Received ${msg.title}: ${msg.message}`);
    if (msg.app.asleep || msg.app.closed) {
      // The app is being opened from a notification
    } else {
      // The app was already open when the notification was received
    }
  });
  ...
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM