gpt4 book ai didi

push-notification - 应用程序在后台时的 ngCordova/Ionic 推送通知

转载 作者:行者123 更新时间:2023-12-04 15:33:31 25 4
gpt4 key购买 nike

我目前正在使用 ionic/ngcordova 构建一个 android 应用程序。我正在实现推送通知。我已经将推送通知实现为在 app.run(function(){..}) 注入(inject)的服务。阶段。注册部分有效,我收到一个包含 regid 的回调。 .此外,当应用程序处于事件状态时,会引发事件并接收通知。

我遇到的问题是,当应用程序进入后台时,根本没有收到通知。我希望当应用程序未运行或类似情况时会发出本地通知,但绝对没有任何 react ,这很奇怪。

在过去的几天里,我一直在网上寻找解决方案,但我一直找不到任何对我表明它应该可以工作的东西。

以下是我的应用程序中的 notificationService.js

app.factory('notificationService', ['$cordovaPush', function($cordovaPush){

var dataFactory = {};

//
// When the device is ready and this service has been plumbed in...
document.addEventListener("deviceready", function(){

console.log("initializing push notifications...");

_register();

}, false);


//
// Registers the device for push notifications...
var _register = function(){

var config = {};

if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

// TODO: centralise this value as it can change...
config = {
senderID: "448168747432",
ecb: "onNotificationGCM"
};

}else {
// iOS
config = {
"badge":"true",
"sound":"true",
"alert":"true"
};

// Can add the following property to the config object to raise a callback with the information if need be...
// "ecb": "onNotificationRegisterAPN"
}

$cordovaPush.register(config).then(function(result){

//
// Typically returns "ok" for android and devicetoken for iOS
console.log(result);

});
};

window.onNotificationGCM = function(result){

console.log(result);

/*
I get called when the app is in the foreground, but nothing happens when the app is in the background.
*/

};

dataFactory.register = _register;

return dataFactory;
}]);

如果有帮助,我将通过 .net 应用程序使用 PushSharp 来传递通知。任何帮助将不胜感激。

更新:我正在使用以下框架/库:
  • ionic 框架 1.2.14-beta6
  • Cordova 4.2.0
  • 推送插件
  • 最佳答案

    对于像我一样连续几天一直在拔头发的其他人,解决方案非常简单……我在 Pushsharp QueueNotification 请求中缺少两个属性。因此,使用 PushSharp github repo 中给出的示例:https://github.com/Redth/PushSharp#sample-code

    push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE-REGISTRATION-ID-HERE").WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));

    需要更新以添加缺少的属性:
    push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
    .WithJson(@"{""alert"":""This is the future"",""badge"":7,""sound"":""sound.caf"",""title"":""Status Bar title"",""message"":""Some text you want to display to the user""}"));

    否则,如果您的应用程序碰巧是使用 Cordova 开发的,并且它当前不在前台,那么什么都不会发生,重复什么都不会发生。

    gdelavald 致谢他对 PushPlugin 的评论为我指明了正确的方向:

    https://github.com/phonegap-build/PushPlugin/issues/212

    关于push-notification - 应用程序在后台时的 ngCordova/Ionic 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467083/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com