【问题标题】:how to generate local notification in ios 10 when your app in forground? When your app in forground then it's not work当您的应用程序在前台时,如何在 ios 10 中生成本地通知?当您的应用程序在前台时,它就不起作用
【发布时间】:2016-12-14 08:56:46
【问题描述】:

我在我的应用程序中执行以下步骤

在我的 appdelegate

#import <UserNotifications/UserNotifications.h>


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
      center.delegate = (id)self;

    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {


                              // Enable or disable features based on authorization.
                          }];







    return YES;
}

在我的 viewcontroller 中生成本地通知的代码

-(IBAction)btnclicked:(id)sender 
{
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = @"gg";
     content.subtitle = @"ggg";
    content.body = @"hhh";
    content.sound = [UNNotificationSound defaultSound];

    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:5 repeats:NO];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"2"
                                                                          content:content trigger:trigger
                                      ];

    // Schedule the notification.
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"hii");
    }];





}

现在,当我的应用在 前台 未生成通知时。

我在 ios 9 中使用

 UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.soundName = @"Default";
    localNotification.alertBody = @"my notification;)";
    localNotification.fireDate = [NSDate date];
    localNotification.category = @"Email";
    localNotification.repeatInterval = kCFCalendarUnitDay;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

此代码在 ios 9 中有效,但在 ios 10 中 UILocalNotification 已弃用。

那么当您的应用在前台时,如何在 ios 10 中生成本地通知?

【问题讨论】:

标签: ios objective-c


【解决方案1】:

尝试在iOS10中与UNUserNotificationCenterDelegate一起使用

iOS 10 添加了UNUserNotificationCenterDelegate 协议,用于在您的应用处于前台时处理通知。

Hope this will help

【讨论】:

  • 我已经在使用用户通知框架。我的问题是当您的应用在前台时如何在 ios 10 中生成本地通知?
【解决方案2】:

要在应用程序处于前台时显示横幅消息,请使用以下方法。

iOS 10,斯威夫特 3:

// 应用在前台收到推送通知时调用该方法

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler(
       [UNNotificationPresentationOptions.alert,
        UNNotificationPresentationOptions.sound,
        UNNotificationPresentationOptions.badge])
}

【讨论】:

  • willPresent 通知:在本地通知中工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 2017-06-26
  • 2018-08-03
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多