【问题标题】:How to implement interactive remote notifications in iOS Objective-c如何在 iOS Objective-c 中实现交互式远程通知
【发布时间】:2017-12-05 09:22:08
【问题描述】:

我是 Objective-c 的新手。我想创建一个交互式远程通知,它有两个动作,一个是调用“OK”,另一个是调用“VIEW”。当用户通过 APNS 收到通知时,如果用户单击“确定”,则必须关闭通知,并且如果用户单击“查看”,则该时间打开我的应用程序的特定页面。就是这样。

我浏览了许多网络文档并掌握了一些通知负载的概念。但我无法明智地实施这一步。任何人都可以帮助我,我该如何实现这个功能。并且请任何人不要标记为重复的问题。谢谢

【问题讨论】:

标签: ios objective-c push-notification ios8 xcode7.3


【解决方案1】:

注意:我在这里使用了我自己的常量,比如KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID 在那个地方使用你的

注册推送

- (void) registerPushNotification {

    UIUserNotificationType  type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |  UIUserNotificationTypeSound;
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObjects:[self createActionNotificationsSettingForApproveBID],[self createActionNotificationsSettingForCancelingRequest ], nil]];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

}


- (UIMutableUserNotificationCategory *) createActionNotificationsSettingForApproveBID {

    UIMutableUserNotificationAction *actionApproveBID = [[UIMutableUserNotificationAction alloc]  init];
    [actionApproveBID setIdentifier:KNotificatoin_IDENTIFER_ACTION_APPROVEBID];
    actionApproveBID.activationMode = UIUserNotificationActivationModeForeground;
    actionApproveBID.title = @"Approve";
    actionApproveBID.authenticationRequired = true;
    [actionApproveBID setDestructive: false];


    UIMutableUserNotificationAction *actionCancelDialog = [[UIMutableUserNotificationAction alloc]  init];
    [actionCancelDialog setIdentifier:KNotificatoin_IDENTIFER_ACTION_DETAILS];
    actionCancelDialog.activationMode = UIUserNotificationActivationModeForeground;
    actionCancelDialog.title = @"Details";
    actionCancelDialog.authenticationRequired = true;
    [actionCancelDialog setDestructive: false];

    UIMutableUserNotificationCategory *cateogoryApproveBID = [[UIMutableUserNotificationCategory alloc] init];
    cateogoryApproveBID.identifier = KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID;
    [cateogoryApproveBID setActions:@[actionApproveBID,actionCancelDialog] forContext:UIUserNotificationActionContextDefault];
    [cateogoryApproveBID setActions:@[actionApproveBID,actionCancelDialog] forContext:UIUserNotificationActionContextMinimal];

    return cateogoryApproveBID;
}

- (UIMutableUserNotificationCategory *) createActionNotificationsSettingForCancelingRequest {

    UIMutableUserNotificationAction *actionGetMoreBids = [[UIMutableUserNotificationAction alloc]  init];
    [actionGetMoreBids setIdentifier:KNotificatoin_IDENTIFER_ACTION_APPROVEBID];
    actionGetMoreBids.activationMode = UIUserNotificationActivationModeForeground;
    actionGetMoreBids.title = @"Get more bids";
    actionGetMoreBids.authenticationRequired = true;
    [actionGetMoreBids setDestructive: false];


    UIMutableUserNotificationAction *actionEditRequest = [[UIMutableUserNotificationAction alloc]  init];
    [actionEditRequest setIdentifier:KNotificatoin_IDENTIFER_ACTION_EDIT_REQUEST];
    actionEditRequest.activationMode = UIUserNotificationActivationModeForeground;
    actionEditRequest.title = @"Edit request";
    actionEditRequest.authenticationRequired = true;
    [actionEditRequest setDestructive: false];

    UIMutableUserNotificationCategory *categoryCancelRequest = [[UIMutableUserNotificationCategory alloc] init];
    categoryCancelRequest.identifier = KNotificatoin_IDENTIFER_ACTION_MORE_BIDS;
    [categoryCancelRequest setActions:@[actionGetMoreBids,actionEditRequest] forContext:UIUserNotificationActionContextDefault];
    [categoryCancelRequest setActions:@[actionGetMoreBids,actionEditRequest] forContext:UIUserNotificationActionContextMinimal];

    return categoryCancelRequest;
}

你将如何处理动作?

- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    NSLog(@" APPLICATION STATUS %ld",(long)[UIApplication sharedApplication].applicationState);

    if ([[[userInfo objectForKey:@"aps"] objectForKey:kCategory] isEqualToString:KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID]){
        if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_APPROVEBID]) {
            NSMutableDictionary *dictData = [NSMutableDictionary dictionaryWithDictionary:userInfo];
            [dictData setObject:@17 forKey:kType];

            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }
        }
        else if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_DETAILS]) {
            NSLog(@"You chose action 2.");
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:userInfo];
            } else {
                [self navigatateAsPerPush:userInfo allowInActiveState:NO];
            }
        }
    } else  if ([[[userInfo objectForKey:@"aps"] objectForKey:kCategory] isEqualToString:KNotificatoin_IDENTIFIER_NOTIFICATION_REQUEST]){

        NSMutableDictionary *dictData = [NSMutableDictionary dictionaryWithDictionary:userInfo];

        if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_EDIT_REQUEST]) {
            NSLog(@"You chose action 1.");
            [dictData setObject:@16 forKey:kType];
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }

        }
        else if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_MORE_BIDS]) {
            NSLog(@"You chose action 2.");
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }

        }

    }

    if (completionHandler) {
        completionHandler();
    }
}

希望对你有帮助

【讨论】:

  • @Swarup 我没有那个。请参考苹果文档
  • 我不明白你的示例代码中有多少动作(按钮)。你能告诉我还有名字吗?所以这对我很有帮助。谢谢
  • 看名字createActionNotificationsSettingForApproveBID就很清楚了,两个按钮approve和cancel,你自己试试那个code你会很容易理解的
  • @Swarup 欢迎,如果回答对您有帮助,您可以批准为已接受!
  • 是的,我会在成功实施后这样做。
【解决方案2】:

借助 iOS 12 SDK,您的应用可以利用 通知中的交互式控件

通知内容应用扩展现在支持自定义视图中的用户交互。如果您的应用通知的内容需要提示用户交互,请添加按钮和开关等控件。

启用用户交互:

打开通知内容扩展的 info.plist 文件。

将 UNNotificationExtensionUserInteractionEnabled 键添加到您的扩展属性。给它一个布尔值,设置为 YES。

这里是参考,想了解更多

https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications

https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications

https://developer.apple.com/documentation/usernotificationsui

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    相关资源
    最近更新 更多