繁体   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