首页 文章

无声的本地通知IOS

提问于
浏览
11

我们可以在IOS应用程序中获得静默本地通知吗?在后台进行一些数据处理而不与用户进行交互 .

我想要做的是创建一个静默的本地通知,每天早上8点开始触发,用户收到它之后我想做一些数据处理并重新创建一个新用户,用户可以看到我查看后处理的新数据第一个静默的本地通知 .

我尽量避免使用推送通知 .

1 回答

  • 4

    您可以在iOS上的后台接收静默通知,但您需要一台服务器来实际发送通知 .

    要执行此操作,请在目标的“功能”选项卡中启用 Remote notifications 后台模式:

    Screenshot of Background Modes settings

    然后在 application:didFinishLaunchingWithOptions: 中注册推送通知

    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    

    在用户允许您的应用发送推送通知之前,您将收到设备令牌:

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
    

    如果出现问题,将调用失败处理程序:

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:
    

    您将 deviceToken 发送到您的服务器并告诉它在设备的当地时间早上8点向该号码发送静音推送通知 .

    该设备将具有以下app委托方法:

    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
    

    并且您将能够进行数据处理 .

    简单!

    完成后别忘了调用完成处理程序!

相关问题