gpt4 book ai didi

ios - 没有收到推送通知(并且没有故障排除想法)

转载 作者:行者123 更新时间:2023-11-29 01:03:55 27 4
gpt4 key购买 nike

我有一个看起来应该是 iOS 应用程序中的工作推送通知。这是它的核心。 让我发疯的是,一切似乎都正常,除了我的断点从未在消息接收器函数中命中;使用 Google Cloud Notifications 的设备注册成功,初始化代码似乎有效,我什至从 GCM 服务收到“成功”消息。

仍然,我似乎从来没有真正在我的应用程序中收到消息。请注意,我在连接到我的 Mac 的 iPad 上运行它,并且通知设置中的所有内容似乎都是正确的/启用的。此外,我正在使用开发证书来匹配 kGGLInstanceIDAPNSServerTypeSandboxOption:true 选项。

我可能遗漏了什么?

import UIKit
import Google

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {

var window: UIWindow?
var gcmSenderID: String?
var registrationToken: String?
var registrationOptions = [String: AnyObject]()

let messageKey = "onMessageReceived"
let registrationKey = "onRegistrationCompleted"

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//********************************
//*** Never Hits Breakpoint ******
//********************************

print("Notification received: \(userInfo)")
// Handle the received message

NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
}

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
//********************************
//*** Never Hits Breakpoint ******
//********************************

NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID

// ...
// Register for remote notifications
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

// Override point for customization after application launch.
return true
}


func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self

// Start the GGLInstanceID shared instance with that config and request a registration
// token to enable reception of notifications
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:true]
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)

}

func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
error: NSError ) {
print("Registration for remote notification failed with error: \(error.localizedDescription)")
// [END receive_apns_token_error]
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
registrationKey, object: nil, userInfo: userInfo)
}

func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
print("Registration Token: \(registrationToken)")
let userInfo = ["registrationToken": registrationToken]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
} else {
print("Registration to GCM failed with error: \(error.localizedDescription)")
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
}
}

func onTokenRefresh() {
// A rotation of the registration tokens is happening, so the app needs to request a new token.
print("The GCM registration token needs to be changed.")
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
}
}

消息发送至https://gcm-http.googleapis.com/gcm/send .有效载荷是任意的/从样本中复制的。

{ 
"content-available":1,
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "<<the registration token resulting from the code above>>"
}

...结果是回复:

{
"multicast_id": 12345...,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "some ID"
}
]
}

有什么想法吗?

最佳答案

请尝试在您的终端中执行以下命令,替换为您的 GCM 注册 token 服务器 API key 。此命令对我有帮助。

Please make sure payload dictionary must be notification not data

curl --header "Authorization: key=” https://android.googleapis.com/gcm/send -d '{"to":"","priority":"high", "notification":{"title":"TEST TITLES"," body ”:“哇!您测试的第一条消息。","badge":"1","sound":"default"}}'

也可以尝试通过应用在后台发送检查 CMD + H

关于ios - 没有收到推送通知(并且没有故障排除想法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36657031/

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