IOS 通知显示两次默认和我的通知警报

Posted

技术标签:

【中文标题】IOS 通知显示两次默认和我的通知警报【英文标题】:IOS notification showing twice with default and my notification alert 【发布时间】:2016-07-25 05:42:34 【问题描述】:

我不是 ios 程序员,所以请多多包涵。我能够收到推送通知,但我在屏幕上收到两个警报。

问题 1: -我在下面创建的UIAlertView* alertWindow - 第二个似乎是默认的通知警报窗口。创建第二个窗口我做错了什么?

问题 2: 当设备被锁定时,我可以在屏幕上看到通知,但这是默认的通知窗口。我在didReceiveRemoteNotification 中没有看到包含我收到的详细信息的通知。

另外,如果有人能解释didFinishLaunchingWithOptions 中的if else 块,我将不胜感激。我复制粘贴了。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    NSString *AMAZON_SERVER = @"xxxxxx";

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) 
        configuration.applicationId = APPLICATION_ID;
        configuration.clientKey = CLIENT_KEY;
        configuration.server = AMAZON_SERVER;
    ]];

    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

    [Fabric with:@[[Digits class]]];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    
    else
    
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

    if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]!=TRUE)
    
        [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"FirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    //storedevice Type in standardUserDefaults
    [self setDeviceType];
    return YES;


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
    [application registerForRemoteNotifications];


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];


- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err 
    NSLog(@"Error in registration. Error: %@", err);


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    [PFPush handlePush:userInfo];
    if ([userInfo objectForKey:@"aps"]) 
        NSMutableDictionary * apsData = [userInfo objectForKey:@"aps"];
        NSString* alert = [apsData objectForKey:@"alert"];
    ...
    ...

        UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
                                                    message: message
                                                   delegate: self
                                          cancelButtonTitle: @"OK"
                                          otherButtonTitles: nil];

        [alertWindow show];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
     else 
        [PFPush handlePush:userInfo];
    

【问题讨论】:

您已注册两次。所以问题就出现了 请检查 didrecieve 通知中的设备状态,如果它处于活动状态,则显示警报,否则不显示。因为处于活动状态的通知横幅不会显示。 @HariKrishnan.P 你能指出我在哪里注册了两次吗? @SagarShirbhate 如何检查设备状态,你的意思是didReceiveRemoteNotification -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo application.applicationIconBadgeNumber = 0; // 应用程序状态。 if (application.applicationState == UIApplicationStateActive) // 如果 applicationState 为 Inactive 则无事可做,iOS 已显示警报视图。 [警报视图显示]; 【参考方案1】:
 First uninstall the app and try it. It will work fine same problem i am facing.

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


    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) 
        configuration.applicationId = APPLICATION_ID;
        configuration.clientKey = CLIENT_KEY;
        configuration.server = AMAZON_SERVER;
    ]];

    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

    [Fabric with:@[[Digits class]]];



    if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]!=TRUE)
    
        [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"FirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    //storedevice Type in standardUserDefaults
    [self setDeviceType];
       #here i am edited

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
   


    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];


else


    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];






    return YES;

// remove the below methods

    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
    [application registerForRemoteNotifications];


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];


- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err 
    NSLog(@"Error in registration. Error: %@", err);


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    //[PFPush handlePush:userInfo];
    if ([userInfo objectForKey:@"aps"]) 
        NSMutableDictionary * apsData = [userInfo objectForKey:@"aps"];
        NSString* alert = [apsData objectForKey:@"alert"];
    ...
    ...

        UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
                                                    message: message
                                                   delegate: self
                                          cancelButtonTitle: @"OK"
                                          otherButtonTitles: nil];

        [alertWindow show];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
     else 
        [PFPush handlePush:userInfo];
    

【讨论】:

【参考方案2】:

你的if语句说如果你的[UIApplication sharedApplication]实例实现了registerUserNotificationSettings:方法,那么它将执行if块,否则它将执行else块。

【讨论】:

以上是关于IOS 通知显示两次默认和我的通知警报的主要内容,如果未能解决你的问题,请参考以下文章

iOS 推送通知 - 如何在没有声音的情况下指定警报和徽章?

iOS 推送通知警报按钮

如何显示自定义警报以在 ios 中启用通知

Android 默认音调选择器问题(带有通知和警报的默认音调)

在我的 iOS 应用程序的本地通知中实现振动和声音设置

iOS 8 交互式通知默认不显示按钮