2016-09-21 114 views
0

在Ionic 2中,我試圖處理用戶當前在應用程序中的事件,以便我可以彈出警告消息並導航到不同的視圖。在我的應用程序構造函數中,我有以下代碼:當在Ionic 2中打開應用程序時處理推送通知

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 

回答

0

它看起來像我不得不在CloudSettingspush.pluginConfig.android.senderID添加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到下面,推出現通知,要穩定得多:

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 
    } 
    }); 
    ... 
}); 
+0

如果你能做到推送通知,請更新您的答案,因爲很多人和我一樣在推送通知 –

+0

都在努力@mohangopi我已經更新了答案。直到明天,我都不能將它標記爲答案。看來你每次打開應用程序都必須註冊通知。我還沒有挖掘出足夠的數據,但這裏是'register'的代碼https://github.com/driftyco/ionic-cloud/blob/master/src/push/push.ts#L214 –

相關問題