给iOS程序添加push代码

本文档详细介绍了如何为iOS应用程序添加推送通知功能。主要内容包括:注册设备接收推送、实现回调函数、处理活跃状态下的推送通知等关键步骤。
给iOS程序添加push代码  Adding Code for a Push Enabled iOS Application

现在,我们开始开发项目,为了使该App能够接受push通知,我们需要对程序进行一些修改。
We are now ready to start programming. We need to make a few modification to the app delegate in order to receive push notifications.

1. 给当前设备注册push通知,在App delegete中的-application:didFinishLaunchingWithOptions: 调用方法:[application registerForRemoteNotifications]
To register the current device for push, call the method[application registerForRemoteNotifications] in the app delegate's-application:didFinishLaunchingWithOptions: method.
/*代码*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ...

  // Register for Push Notitications, if running iOS 8
  if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |

                                                    UIUserNotificationTypeBadge |

                                                    UIUserNotificationTypeSound);

    UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:userNotificationTypes

                                                                            categories:nil];

    [application registerUserNotificationSettings:settings];

    [application registerForRemoteNotifications];

  } else {

    // Register for Push Notifications before iOS 8

    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

                                                     UIRemoteNotificationTypeAlert |

                                                     UIRemoteNotificationTypeSound)];

  }
  ...
}

2. 如果上一步的注册push成功,那么回调函数method-application:didRegisterForRemoteNotificationsWithDeviceToken在
APPdelegate中被调用。我们需要实现这个方法,并用它来通知解析这种新设备。
If the registration is successful, the callback method-application:didRegisterForRemoteNotificationsWithDeviceToken: in the application delegate will be executed. We will need to implement this method and use it to inform Parse about this new device.
- (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.channels = @[@"global" ];


  [currentInstallation saveInBackground];
}

3. 当一个push通知过来的时候,程序不在前台,该通知则显示在iOS系统通知中心,然而,如果收到push通知的时候,app是active的
为了实现,该通知能在APP上面弹出来,我们可以实现:app的delegate方法[application:didReceiveRemoteNotification]

这种情况,我们简单调用解析,解析器创建一个模态alert并显示push的内容。

另外:如果程序完全退出,先调用didFinishLaunchingWithOptions把程序启动起来。

When a push notification is received while the application is not in the foreground, it is displayed in the iOS Notification Center. However, if the notification is received while the app is active, it is up to the app to handle it. To do so, we can implement the [application:didReceiveRemoteNotification] method in the app delegate. In our case, we will simply ask Parse to handle it for us. Parse will create a modal alert and display the push notification's content.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

  [PFPush handlePush:userInfo];

}

4. 现在,你可启动APP检查一下上面的逻辑是否都设置正确了。如果正确,第一次启动APP的时候你应该会看到一个模态alert来询问用户

是否允许发送push通知。

You should now run your application (on your iOS device) to make sure everything is set up correctly. If it is, the first time you run this app you should see a modal alert requesting permission from the user to send push notifications.

原贴地址:https://parse.com/tutorials/ios-push-notifications
github地址:https://github.com/ParsePlatform/PushTutorial

### uniapp iOS 离线打包集成 UniPush 方法 #### 准备工作 为了成功完成uni-app应用在iOS平台上的离线打包并集成UniPush功能,开发者需确保环境已准备好。这包括但不限于安装最新版的HBuilderX工具以及获取必要的证书和描述文件用于签名[^1]。 #### 修改 `manifest.json` 文件 尽管对于本地构建而言,在`manifest.json`中的设置可能不会直接影响到最终产物的功能实现(如推送服务),但仍建议按照官方文档指导正确填写相关字段以保持一致性。特别是当项目后续可能会采用云端编译时这些配置项将会生效[^2]。 #### 更新 Info.plist 配置 针对iOS端的应用程序,需要编辑项目的`Info.plist`来加入Getui SDK所需的信息条目。具体来说就是增加与推送通知有关的服务权限声明,并指定远程通知类型的键值对;同时也要记得添加个推特有的参数以便于初始化过程中能够顺利连接至服务器进行消息接收操作。 ```xml <key>UNIPUSH_APPID</key> <string>Your App ID Here</string> <key>UNIPUSH_APPKEY</key> <string>Your App Key Here</string> <!-- Other necessary configurations --> ``` #### 处理 Push 功能兼容性问题 如果遇到由于升级到了新的UniPush版本而导致原有代码执行时报错的情况,则可以通过调整部分逻辑或者尝试不同的API调用来解决问题。例如取消不必要的选项选择——像是否启用离线包形式的支持等——从而简化流程减少潜在冲突的可能性。 #### 构建与测试 最后一步是在真机上部署应用程序之前先通过模拟器进行全面检测。确认所有预期行为都能正常运作之后再考虑发布正式版本给更多用户群体试用反馈意见不断优化改进产品体验质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值