菜鸟教程小白 发表于 2022-12-12 22:17:41

ios - 如何在应用程序处于后台时处理 iOS 远程通知


                                            <p><p>我正在通过苹果推送通知开发 iOS 推送通知功能,现在我在我的应用程序处于后台或前台时收到适当的通知,但是我想在我的应用程序处于后台时处理远程通知,基本上当我的应用程序运行时在后台它只是显示来自有效负载的警报消息。实际上我只是想自定义我的远程通知。</p>

<p>代码:</p>

<pre><code> - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
if ([[ systemVersion] floatValue] &gt;= 8.0)
{
//      [ registerUserNotificationSettings:];
[ registerForRemoteNotifications];
}
else
{
[ registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@&#34;Did Register for Remote Notifications with Device Token (%@)&#34;, error);
}
- (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken {
NSLog(@&#34;Did Register for Remote Notifications with Device Token (%@)&#34;, deviceToken);
}
-(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (UIBackgroundFetchResult))completionHandler
{
NSDictionary * aps=;
NSLog(@&#34;did recevie %@&#34;,aps);
NSLog(@&#34;userinfo details %@&#34;,);
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 iOS 10 中,首先你必须在 <code>AppDelegate.h</code> 文件中设置 <code>UNUserNotificationCenterDelegate</code></p>

<pre><code>@interface AppDelegate : UIResponder &lt;UIApplicationDelegate,CLLocationManagerDelegate,UNUserNotificationCenterDelegate&gt;
</code></pre>

<p>之后在 <code>AppDelegate.m</code> 中编写这样的代码</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[ systemVersion] floatValue] &gt;= 7.1) {
            // iOS 7.1 or earlier. Disable the deprecation warnings.
            UIRemoteNotificationType allNotificationTypes =
            (UIRemoteNotificationTypeSound |
             UIRemoteNotificationTypeAlert |
             UIRemoteNotificationTypeBadge);
            ;
            [ registerForRemoteNotifications];
      } else {
            // iOS 8 or later
            //
    if ([[ systemVersion] floatValue] &gt;= 9.0)
    {
                UIUserNotificationType allNotificationTypes =
                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
                UIUserNotificationSettings *settings =
                ;
                ;
                [ registerUserNotificationSettings:settings];
                [ registerForRemoteNotifications];
                ;
            } else {
                // iOS 10 or later
    #if defined(__IPHONE_10_0) &amp;&amp; __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= __IPHONE_10_0
                UNAuthorizationOptions authOptions =
                UNAuthorizationOptionAlert
                | UNAuthorizationOptionSound
                | UNAuthorizationOptionBadge;
                [
               requestAuthorizationWithOptions:authOptions
               completionHandler:^(BOOL granted, NSError * _Nullable error) {
               }
               ];
                // For iOS 10 display notification (sent via APNS)
                [ setDelegate:self];            
    [ registerForRemoteNotifications];
         return YES;
}
</code></pre>

<p>现在iOS10以下版本实现这个方法</p>

<pre><code>    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
      NSLog(@&#34;Notification received: %@&#34;, userInfo);
      if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @&#34;10.0&#34; ) )
      {
            NSLog( @&#34;iOS version &gt;= 10. Let NotificationCenter handle this one.&#34; );
            return;
      }
      NSLog( @&#34;HANDLE PUSH, didReceiveRemoteNotification: %@&#34;, userInfo );
          else{
            handler( UIBackgroundFetchResultNewData );
}

    }
</code></pre>

<p>Apple 在 iOS10 中引入了这两种方法来接收推送通知。</p>

<p>也写这些方法</p>

<pre><code>    // Receive displayed notifications for iOS 10 devices.
    #if defined(__IPHONE_10_0) &amp;&amp; __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= __IPHONE_10_0
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
         willPresentNotification:(UNNotification *)notification
             withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

      NSDictionary *userInfo = notification.request.content.userInfo;

      NSLog(@&#34;%@&#34;, userInfo);

            completionHandler( UNNotificationPresentationOptionAlert );
   }

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    NSLog(@&#34;Userinfo %@&#34;,response.notification.request.content.userInfo);
//    completionHandler(UNNotificationPresentationOptionAlert);
       }
</code></pre>

<p>就是这样。</p>

<p>试试这个</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在应用程序处于后台时处理 iOS 远程通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39719528/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39719528/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在应用程序处于后台时处理 iOS 远程通知