iOS收到推送,设置通知栏“显示”与“不显示”

2019-07-12  本文已影响0人  路漫漫其修远兮Wzt

一、设置某条推送通知在通知栏是否展示的参数值UNNotificationPresentationOptionNone:
(1)设置某条推送消息,在通知栏展示

UNNotificationPresentationOptions option = UNNotificationPresentationOptionBadge | 
UNNotificationPresentationOptionSound | 
UNNotificationPresentationOptionAlert;

(2)设置某条推送消息,不在通知栏展示

UNNotificationPresentationOptions option = UNNotificationPresentationOptionNone;

二、上代码
1.AppDelegate文件中,系统通知代理回调的处理

#pragma mark - UNUserNotificationCenterDelegate
//前台收到通知
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    
    [WTNotiUtil handleForegroundPush:center willPresentNotification:notification withCompletionHandler:completionHandler];
}

//用户点击通知
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    
    [WTNotiUtil handleClickPush:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_

-(void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(nonnull NSDictionary *)userInfo completionHandler:(nonnull void (^)())completionHandler{
    if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){
        NSLog(@"ACCEPT_IDENTIFIER is clicked");
    }
    completionHandler();
}
#endif

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    static NSInteger i = 0;
    static NSInteger j = 0;
    NSLog(@"+++ didReceiveRemoteNotification : %@", userInfo);
    printf("%ld>>>>----%ld",i,j);
    if (application.applicationState == UIApplicationStateActive) {
        NSLog(@"active");
        [WTNotiUtil handleForegroundPush:userInfo cb:^(WTBaseSCTNoti *basebnoti) {
            if([basebnoti isKindOfClass:WTPushPayloadMod.class]){
                PushPayloadMod *mod = (WTPushPayloadMod *)basebnoti;
                [iPop toastWarn:mod.bodyContent];
            }
        }];
    }
    else if(application.applicationState == UIApplicationStateInactive)
    {
        j++;
        NSLog(@"inactive");
        [BCNotiUtil handleClickPush:userInfo response:0 cb:0];

    }else if(application.applicationState==UIApplicationStateBackground){
        i++;
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

2.自定义类WTNotiUtil,处理某条推送消息是否在通知栏展示的逻辑
WTNotiUtil类:

+(void)handleForegroundPush:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

     __block UNNotificationPresentationOptions option=UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert;
    UNNotificationPresentationOptions *optionPointer=&option;
    //作用域结束时,执行的代码块
    onExit{
        completionHandler(*optionPointer);
    };
    
    NSDictionary *userinfo = notification.request.content.userInfo;
    UNNotificationRequest *request = notification.request;
    UNNotificationContent *content = request.content;
    
    NSString *title = content.title;
    NSString *subtitle =content.subtitle;
    NSString *body=content.body;
    NSString *categoryIdentifier = content.categoryIdentifier;
    UNNotificationSound *sound = content.sound;
    NSNumber *badge = content.badge;
    NSString *threadIdentifier = content.threadIdentifier;
    
    
    
    if([notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]){
        //远程通知
        [self handleForegroundPush:userinfo cb:^(BaseSCTNoti *basenoti) {
            //设置不在通知栏提示
            if(basenoti && !basenoti.showPushWhenForeground){
                option=UNNotificationPresentationOptionNone;
            }
        }];
        
    }else{
        //本地通知
 
        NSLog(@"iOS10 收到本地通知:{\n title:%@,\n subtitle:%@,\n body:%@,\n categoryIdentifier:%@,\n sound:%@,\n badge:%@,\n threadIdentifier:%@,\n launchImageName:%@,\n attachments:%@,\n userInfo:%@\n}",title,subtitle,body,categoryIdentifier,sound,badge,threadIdentifier,content.launchImageName,content.attachments,userinfo);
    }
    
}
上一篇 下一篇

猜你喜欢

热点阅读