Dal*_*vik 8 iphone objective-c push-notification ios xcode6
我在我的应用程序中实现了推送通知.
当我的应用程序在前台时,我的应用程序正常工作.
但当应用程序在后台或被杀时,我的didReceiveRemoteNotification被叫两次.
我已经提出了一个处理Push通知并从didFinishLaunchingWithOptions和 调用此方法的常用方法didReceiveRemoteNotification
这是我的实施:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *pushDictionary = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary) {
[self customPushHandler:pushDictionary];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
而且:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
[self customPushHandler:userInfo];
}
Run Code Online (Sandbox Code Playgroud)
和:
- (void) customPushHandler:(NSDictionary *)notification {
// Code to push ViewController
NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner
}
Run Code Online (Sandbox Code Playgroud)
当我的应用程序运行时,ViewController被推送一次.当我打开我的应用程序从通知横幅,然后我的屏幕被推了两次.
我放置了一个NSLog,customPushHandler当App处于前台时,我得到了一次,当我从Notification banner中启动时,我得到了两次.
我的代码中有什么问题.
小智 6
“当应用程序在后台时,该方法会被调用两次,一次是在您收到推送通知时,另一次是在您点击该通知时。 ”
如果应用程序在后台,您可以验证应用程序状态并忽略收到的通知。
解决方法:远程通知方法调用两次
斯威夫特 4 代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if (application.applicationState == .background) {
completionHandler(.noData)
return
}
// logic
// call completionHandler with appropriate UIBackgroundFetchResult
}
Run Code Online (Sandbox Code Playgroud)
检查didReceiveRemoteNotification中的这一条件,就像您在应用程序处于后台时点击通知一样。这样,您就可以避免被两次呼叫。
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive || state == UIApplicationStateForeground || state == UIApplicationStateActive)
{
//Do checking here.
[self customPushHandler:userInfo];
}
}
Run Code Online (Sandbox Code Playgroud)
检查我是否已编辑我的答案。
| 归档时间: |
|
| 查看次数: |
3227 次 |
| 最近记录: |