1

我使用下面的推送通知插件在Ionic2上(“通知”)事件的PhoneGap-插件推不点火当应用程序在后台

http://ionicframework.com/docs/native/push/

预期的行为: 当应用程序被关闭,并收到通知,并且当用户点击通知时,on(“通知”)事件应该在应用程序打开后触发。

实际行为: 我收到通知成功。但是当应用程序处于后台或关闭状态时,当我收到通知并点击通知时,(“通知”)事件不会触发。

科尔多瓦版本7.0.1 Android版本6.2.3

我的代码:

this.platform.ready().then(() => { 
    this.pushsetup(); 
}); 

private pushOptions: PushOptions; 
private pushObject: PushObject; 
pushsetup() { 
    // to check if we have permission 
    this.push.hasPermission() 
     .then((res: any) => { 
      if (res.isEnabled) { 
       console.log('We have permission to send push notifications'); 
       // configuration of push notification 
       this.pushOptions = { 
        android: { 
         senderID: 'XXXXXXXXXXX', 
         icon: 'icon_notification' 
        }, 
        ios: { 
         alert: 'true', 
         badge: true, 
         sound: 'false', 
         senderID: 'XXXXXXXXXXX' 
        }, 
        windows: {} 
       }; 
       this.pushObject = this.push.init(this.pushOptions); 

       // attach push events 
       this.storage.get('isPushRegistered') 
        .then(isPushRegistered => { 
         if(!isPushRegistered){ 
          this.pushObject.on('registration').subscribe((registration: any) => { 
           console.log('Device registered', registration) 
           this.storage.set('isPushRegistered', true) 
          }); 
         } 
        }) 


       this.pushObject.on('notification').subscribe((notification: any) => { 
        console.log('Received a notification', notification) 
       }); 
       this.pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error)); 
      } 
     }); 
} 

所以,在我的代码,你可以看到this.pushObject.on ('通知')事件。当应用程序关闭时不会触发。

谢谢你的时间和支持。

+0

你有没有设置“内容-available“:1表示从后端发送的有效负载数据。 – Vasanth

+0

是的,我设定了它。但仍然没有成功。另外,我已经阅读了phonegap插件文档,其统计如下:“如果用户已经杀死了应用程序,则不会进一步处理推送数据。” https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#push-message-arrives-with-app-in-background –

+0

当应用程序关闭时,您将无法执行电话差距推送的任何功能。但是当它在后台时,你可以通过设置“content-available”来执行:1. – Vasanth

回答

2

这不是客户端代码的问题。由于通知有效负载,发生此问题。

从PhoneGap的-插件推的官方文档

通知性能与所接收应用的前景/背景状态和有效载荷发送到应用程序。

例如,如果你发送以下有效载荷: 的PhoneGap-插件推/

{ 
    "notification": { 
    "title": "Test Notification", 
    "body": "This offer expires at 11:30 or whatever", 
    "notId": 10 
    } 
} 

当你的应用程序是在前台,任何对(“通知”),您已注册的处理程序将被调用。但是,如果您的应用程序处于后台,通知将显示在系统托盘中。点击系统托盘中的通知将启动应用程序,但您的on('notification')处理程序将而不是被调用为具有通知载荷的消息不会导致插入onMessageReceived方法被调用。

如果您发送的有效载荷,并发出通知&数据对象的像这样的组合:

{ 
    "notification": { 
     "title": "Test Notification", 
     "body": "This offer expires at 11:30 or whatever", 
     "notId": 10 
    }, 
    "data" : { 
     "surveyID": "ewtawgreg-gragrag-rgarhthgbad" 
    } 
} 

当你的应用程序是在前台任何关于(“通知”),您已注册的处理程序将叫做。如果您的应用程序在后台,通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,并且您的on('notification')处理程序将而不是被调用为具有通知有效载荷的消息不会导致插件onMessageReceived方法被调用。

使用这个插件,当你推载荷我的建议的格式(虽然它从谷歌的文档不同)运作时间的100%

{ 
    "data" : { 
     "title": "Test Notification", 
     "body": "This offer expires at 11:30 or whatever", 
     "notId": 10, 
     "surveyID": "ewtawgreg-gragrag-rgarhthgbad" 
    } 
} 

当你的应用程序是在前台任何你已经注册的('通知')处理程序都会被调用。如果您的应用程序位于后台,则通知将显示在系统托盘中。点击系统托盘通知将启动应用程序,和你的(“通知”)处理称为数据如下:

{ 
    "message": "This offer expires at 11:30 or whatever", 
    "title": "Test Notification", 
    "additionalData": { 
     "surveyID": "ewtawgreg-gragrag-rgarhthgbad" 
    } 
} 

Link to the docs

+0

感谢朋友。这对我有很大帮助。 –

0

下面是代码已为我工作。当应用程序关闭时,您会收到通知。如果你想通过查看一些有效载荷数据来处理点击事件。然后看看下面的代码:

pushObject.on('notification').subscribe((notification: any) => { 

    // this method will be called when you click on notification when app is closed 
    pushObject.finish() 
     .then(e => {}) 
     .catch(e => { console.log("ERROR NOTIFICATION",e); }) 

}).catch(e => { 
    console.log("ERROR NOTIFICATION",e); 
}) 
+0

感谢你的关注,你可以在github上分享你的代码吗?服务器和应用程序代码。你给我的建议我尝试过,但仍然不起作用。 –

+0

我已经创建了一个要点看看它。 https://gist.github.com/coolvasanth/ba61710cb68af4097acfe603765c2df3随时问,如果你有任何疑问。 – Vasanth

0

您可以通过下面的代码检查于地面:

pushObject.on('notification').subscribe((data: any) => { 
     console.log('message -> ' + data.message); 
     //if user using app and push notification comes 
     if (data.additionalData.foreground) { 
     // if application open, show popup 
     } 
     else{ 
     //if user NOT using app and push notification comes 
     } 

有关详细信息发送推送通知的离子,您可以访问:

https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59

相关问题