如何从iWatch触发本地通知才能在iWatch上显示?

时间:2017-06-12 12:08:29

标签: ios objective-c notifications watchkit

我已阅读,我们可以在WatchOS 3的支持下从iWatch发出本地通知。我的要求是在iWatch上显示本地通知,通知源将是iWatch本身。

enter image description here

来源:https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/BasicSupport.html

以下是代码段:

ExtensionDelegate

    @interface ExtensionDelegate () <WCSessionDelegate, UNUserNotificationCenterDelegate>

    @end

    @implementation ExtensionDelegate


    - (void)applicationDidFinishLaunching {
        // Perform any final initialization of your application.

   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){

        }];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self addALocalNotificationForState:@"This is my iWatch notification."];
        });


    }

    -(void) addALocalNotificationForState:(NSString *) message
    {
        UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
        objNotificationContent.body = message;
        objNotificationContent.sound = [UNNotificationSound defaultSound];
        objNotificationContent.badge = 0;

        UNTimeIntervalNotificationTrigger *trigger =  [UNTimeIntervalNotificationTrigger                                             triggerWithTimeInterval:0.1 repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten" content:objNotificationContent trigger:trigger];
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"Local Notification succeeded");
            }else{
                NSLog(@"Local Notification failed");
            }
        }];
    }

当我执行上面的代码时,它工作正常,我甚至得到了日志&#34;本地通知成功&#34;但是唉,iWatch上没有任何通知。我尝试将应用程序保持在前台和后台,但对于这两种情况,它都没有显示任何通知。

有人可以帮我吗?

提前致谢。

0 个答案:

没有答案