簡體   English   中英

事件IOS之后是否可以觸發本地通知?

[英]Is it possible to trigger a local notification after an event IOS?

我正在開發一個應用程序,該應用程序執行需要很長時間的一些繁重的計算任務。 我想在任務完成時通過本地推送通知來通知用戶。 這可能嗎? 我只能在網上找到的唯一信息是在某些時間/日期觸發通知,或者應用程序進入后台或終止,所有這些操作都在appDelegate中完成。 有沒有辦法在我自己的課堂上做到這一點?

謝謝。

我不確定100%確定您要查找UILocalNotification示例,因為帖子標題提到了推送通知。 無論哪種方式,您都可以根據需要安排任何班級的本地通知

- (void)throwLocalNotificationWithMessage:(NSString*)message {    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDate *now = [NSDate date];
    localNotification.fireDate = now;
    localNotification.alertBody = message;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [UIApplication sharedApplication].applicationIconBadgeNumber++;
}

另外,出於我的需要,當區域監視檢測到邊界進入/退出更改時,我會拋出這些本地通知。 該代碼也會在我的應用程序在后台運行時運行,在這種情況下,它們看起來像推送通知。

上述Aaron的回答很好用,但不要忘記請求本地通知的許可。 就我而言,我在以下位置向AppDelegate尋求許可-

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

這是iOS8和iOS9的代碼

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

暫無
暫無

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

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