应用程序最小化时点击获取本地通知文本 ios

Get Local notification text on tap when App is minimize ios

例子:- 我在最小化状态下生成了 4 种类型的 4 个本地通知。

以下通知类型。

  1. 留言

  2. 好友请求

  3. 视频通话

  4. 语音通话

通知显示在通知中心。

现在..我点击了第二条通知,

  1. 如何获取点击了多少条通知?

  2. 如何获取第二个通知正文警报文本(内容)?

this mehod is not working for minimize state

喜欢 whatsApp.. 我需要根据通知类型进入特定屏幕。

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSString *Notitype=@"";
if ([[payload.dictionaryPayload valueForKey:@"type"]isEqualToString:@"video"]) {
    Notitype=@"video";
}
else if ([[payload.dictionaryPayload valueForKey:@"type"]isEqualToString:@"friendRequest"]) {
     Notitype=@"friend Request";
}
else if([[payload.dictionaryPayload valueForKey:@"type"] isEqualToString:@"message"] )
{
    Notitype=@"message";
}
else{
    Notitype=@"audio";
}

我正在这样创建 UILocalNotification :

UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:Notitype];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone  defaultTimeZone]];
[[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

对于将本地通知设置为:

// Schedule the notification
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.repeatInterval = NSDayCalendarUnit;
    [notification setAlertBody:Notitype];
    [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    [notification setTimeZone:[NSTimeZone  defaultTimeZone]];

    //Create user info for local notification.
    NSDictionary* dict = @{@"Type": @"message", @"ID": @"set your unique ID", @"body": notification.alertBody,};
    notification.userInfo = dict;

    //You can Set Type as: message, friend Request, video call, Audio call.

    [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

要获取点击的本地通知类型,您可以使用 didReceiveLocalNotification 委托。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    //[super application:application didReceiveLocalNotification:notification]; // In most case, you don't need this line

    //Get notification type
    NSString *notificationType = [notification.userInfo valueForKey:@"Type"];
    //notificationType as: message, friend Request, video call, Audio call.

    NSString *notificationBody = [notification.userInfo valueForKey:@"Notification_body"];

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        // Application in foreground when notification is delivered.

        if ([notificationType isEqual:@"message"]) {
            //Handle message
        } else if ([notificationType isEqual:@"friend Request"]) {
            //Handle friend Request

        } else if ([notificationType isEqual:@"video call"]) {
            //Handle video call

        } else if ([notificationType isEqual:@"Audio call"]) {
            //Handle Audio call

        }

    } else if (state == UIApplicationStateBackground) {
        // Application was in the background when notification was delivered.
        if ([notificationType isEqual:@"message"]) {
            //Handle message
        } else if ([notificationType isEqual:@"friend Request"]) {
            //Handle friend Request

        } else if ([notificationType isEqual:@"video call"]) {
            //Handle video call

        } else if ([notificationType isEqual:@"Audio call"]) {
            //Handle Audio call

        }

    } else {

    }
}

更新

还要在 DidFinishLaunchingWithOptions 中检查同样的事情,可能用户不会对通知做出反应,而是点击应用程序图标以打开应用程序。因此,如果有视频通话或任何紧急情况,则必须通过 DidFinishLaunchingWithOptions

处理