简体   繁体   English

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

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

I have some strange problem... I'm using push notifications for messaging in my Ionic app but it works from time to time... My code is: 我有一个奇怪的问题...我在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');
            }
        });
    });
}

It is located in app.component.ts . 它位于app.component.ts

Some of behavior: 一些行为:

  1. Works fine when app is minimized on Cat S60 phone Cat S60手机上最小化应用程序时可以正常工作
  2. Some times works when app is minimized on some cheap Huawei 在某些廉价的Huawei手机上最小化应用程序有时会起作用
  3. Some time works on both devices when app is opened as it should be. 在应有的状态下打开应用程序时,这两种设备都需要一些时间。 In most times not. 在大多数情况下不是。

When I send it to Google I receive back OK status. 当我将其发送给Google时,我会返回OK状态。

You can use one signal for notifications. 您可以使用一个信号进行通知。 It's fast and effective. 快速有效。 You can get detailed information from the links below. 您可以从下面的链接中获取详细信息。

https://documentation.onesignal.com/v3.0/docs/cordova-sdk-setup https://documentation.onesignal.com/v3.0/docs/cordova-sdk-setup

https://www.npmjs.com/package/onesignal-cordova-plug https://www.npmjs.com/package/onesignal-cordova-plug

http://www.plugreg.com/plugin/one-signal/onesignal-cordova-sdk http://www.plugreg.com/plugin/one-signal/onesignal-cordova-sdk

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

相关问题 不使用推送通知的Cordova应用程序:“缺少推送通知权利” - Cordova app that doesn't use push notifications: “Missing push notification entitlement” 如何在Cordova平台中实现推送通知? - How to implement push notifications in Cordova Platform? 当应用程序处于空闲状态或处于后台时,如何获取cordova android推送通知? - How to get cordova android push notifications, when the app is in idle state or is in background? 如何在Cordova / Phonegap Android应用程序中使用Parse.com推送通知? - How do I get Parse.com Push Notifications working in a Cordova/Phonegap Android app? 未打开应用程序时,Phonegap或Cordova应用程序会收到推送通知吗? - Will Phonegap or Cordova apps receive push notifications when app not opened? 网络推送通知是否在 Apache Cordova Android 应用程序中工作? - Do Web Push Notifications work in an Apache Cordova Android app? Cordova 5.3.3 Android应用程序在调用“ registerDevice”获取推送通知时崩溃 - Cordova 5.3.3 Android app crashes on calling 'registerDevice' for push notifications 如何使用手机/ Cordova移动应用程序发送通知 - How to send notifications with a phone / Cordova mobile app Cordova中的后台Firebase推送通知 - Background Firebase Push Notifications in Cordova Cordova 3.3中的用户推送通知 - User push notifications in cordova 3.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM