簡體   English   中英

IOS,應用程序中的本地通知 - 無法正常工作

[英]IOS, Local notification in app - not work

我做了一些檢查然后解雇它,但它只在應用程序在后台時工作。

如何在用戶進入應用程序時觸發通知?

我正在使用didReceiveRemoteNotification方法,但其響應僅在后台而不在應用程序中。 我還嘗試在viewDidLoad方法中觸發通知,但它也不起作用。

- (void) application:(UIApplication *)application
  didReceiveRemoteNotification:(NSDictionary *)userInfo
        fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
                UILocalNotification* n1 = [[UILocalNotification alloc] init];
                n1.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
                n1.alertBody = @“test”;
                n1.soundName = @"default";
                [[UIApplication sharedApplication] scheduleLocalNotification: n1];

    completionHandler(UIBackgroundFetchResultNewData);
} 

UILocalNotification設計為在用戶未按照UILocalNotification使用應用程序時觸發,請參閱Apple文檔中的說明

如果您想知道應用程序是否收到了UILocalNotification您可以使用以下代碼。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([notification.alertBody isEqualToString:@"Application is timeout!"])
   {
     // show an alert regarding your notification
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM