簡體   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