菜鸟教程小白 发表于 2022-12-11 17:57:14

ios - 多个 UNUserNotifications 未触发


                                            <p><p>我正在设置多个 UNUsernotifications,如下所示,</p>

<pre><code>- (void)viewDidLoad {
    ;
    notifCount = 0;
    UNUserNotificationCenter *center = ;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                        completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!error) {
                                  NSLog(@&#34;request succeeded!&#34;);

                                  ;

                              }
                        }];
}
</code></pre>

<p>在 <code>set10Notif</code> 方法中,我设置了多个(8 个用于测试)通知,时间距当前时间 10 秒。</p>

<pre><code>-(void) set10Notif
{
    notifCount = notifCount+1;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@&#34;10.0&#34;) &amp;&amp; notifCount &lt; 10)
    {
      // create actions
      NSCalendar *calendar = [ initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

      ];

       NSDate *fireD = [ dateByAddingTimeInterval:notifCount*10];

       NSString *fireStr = ;

       NSDateComponents *components = ;

      UNMutableNotificationContent *objNotificationContent = [ init];
    objNotificationContent.title = ;
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
                                                                        arguments:nil];
    objNotificationContent.sound = ;

      UNCalendarNotificationTrigger *trigger = ;

      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@&#34;Prayer&#34;
                                                                        content:objNotificationContent trigger:trigger];
      UNUserNotificationCenter *userCenter = ;
      [userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
      if (!error) {

            NSLog(@&#34;Local Notification succeeded&#34;);
      }
      else {
            NSLog(@&#34;Local Notification failed&#34;);
      }
      ;
       }];
   #endif
    }
}
</code></pre>

<p><strong>所有本地通知均已设置。但是设备中只有一个通知被触发,这是最后一个。</strong></p>

<p>为什么不能触发多个通知?
我做错了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为每个通知提供不同的“requestWithIdentifier”和“时间延迟”并尝试,可能对您有用。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 多个 UNUserNotifications 未触发,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40277590/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40277590/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 多个 UNUserNotifications 未触发