【发布时间】:2017-01-25 23:44:05
【问题描述】:
使用 TwilioChatClient pod,我已成功将我的应用程序注册到 Twilio Programmable Chat 以接收 APN 通知。
但是,据我所知,这些通知是在实例化的 TwilioChatClient 客户端上调用 client.register(withToken: deviceToken) 后创建的,而不是通过应用程序的 AppDelegate didReceiveRemoteNotification 方法创建的。更奇怪的是,didReceiveRemoteNotification 被调用,但仅在应用程序处于活动状态而不是后台状态时,我想执行一些操作。
有谁知道这些通知是在哪里以及如何创建的,或者为什么didReceiveRemoteNotification 只在活动状态下被调用?除其他外,我想随着客户端发出的每个通知增加应用程序图标徽章编号。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered for notifications");
if UserUtils.client?.userInfo != nil {
print("Has info");
UserUtils.deviceToken = deviceToken;
UserUtils.client?.register(withToken: deviceToken)
} else {
print("No info");
updatedPushToken = deviceToken as NSData?
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Received a notification");
if UIApplication.shared.applicationState != .active {
print(userInfo);
UserUtils.client?.handleNotification(userInfo);
UIApplication.shared.applicationIconBadgeNumber += 1;
if UserUtils.client?.userInfo != nil {
print(userInfo);
let jsonNotification = JSON(userInfo["aps"])
let alert = jsonNotification["alert"].stringValue + "\"}";
print(JSON.init(parseJSON: alert)["body"]);
} else {
print(userInfo);
let jsonNotification = JSON(userInfo["aps"])
let alert = jsonNotification["alert"].stringValue + "\"}";
print(JSON.init(parseJSON: alert)["body"]);
}
} else {
}
}
client.register(withToken: deviceToken) 按预期工作。
【问题讨论】:
-
请添加您的代码sn-ps
-
您使用哪种
didReceiveRemoteNotification方法?你在用这个application(_:didReceiveRemoteNotification:fetchCompletionHandler:)? -
@muescha,我正在使用包含 fetchCompletionHandler 的方法 - 但我已经尝试了这两种方法,但它们都不适合我。我现在将发布我的代码 sn-ps!
-
您还看到文档中的最后一段吗?也许这就是为什么?
-
@muescha,您指的是哪个文档?可以给我链接吗?
标签: ios swift push-notification twilio