簡體   English   中英

如何在Cordova應用程序中使用推送通知?

[英]How to use push notifications in cordova app?

我有一個奇怪的問題...我在Ionic應用程序中使用推送通知進行消息傳遞,但有時會起作用...我的代碼是:

public initPushNotification() {
    if (!this.platform.is('cordova')) {
        console.warn('Cordova is not available - Run in physical device. Push notifications not initialized.');

        return;
    }

    let zone = new NgZone({ enableLongStackTrace: false });

    zone.run(() => {
        this.Push.hasPermission().then((res: any) => {
            if (res.isEnabled) {
                const options: PushOptions = {
                    android: {
                        senderID: GOOGLE_FCM_ID
                    }
                };

                let push_object = this.Push.init(options);

                push_object.on('registration').subscribe((data: any) => {
                    this.APIRequest.post('/messages/register-fcm-token', data).subscribe(response => {
                        console.log(response)
                    }, err => {
                        console.log(err)
                    })

                    console.log('push.registration', data);
                });

                push_object.on('notification').subscribe((data: any) => {
                    console.log('push.notification', data);

                    this.events.publish('messages:push', data.message);
                });

                push_object.on('error').subscribe(error => console.error('Error with Push plugin', error));
            } else {
                alert('We do not have permission to send push notifications');
            }
        });
    });
}

它位於app.component.ts

一些行為:

  1. Cat S60手機上最小化應用程序時可以正常工作
  2. 在某些廉價的Huawei手機上最小化應用程序有時會起作用
  3. 在應有的狀態下打開應用程序時,這兩種設備都需要一些時間。 在大多數情況下不是。

當我將其發送給Google時,我會返回OK狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM