解析推送通知获取通知通道 - iOS

时间:2015-07-31 12:40:16

标签: ios parse-platform push-notification

我最近使用Parse为我的应用设置推送通知。我已经能够收到通知和数据,但现在我想获得通知已发送到的频道。在ios中有没有办法这样做?

2 个答案:

答案 0 :(得分:0)

您查看过本指南了吗?这真的很有帮助。

https://www.parse.com/tutorials/ios-push-notifications

这是一个快速摘要。创建开发和生产证书,然后将它们附加到您希望能够发送到的应用程序的开发者帐户。附上这些证书后重新下载它们,将它们更改为.p12文件w Keychain Access,然后将它们上传到Parse.com。在Xcode中,请确保通过首选项进入您的帐户并进行刷新,以便您拥有更新的配置文件。我会在Xcode的稳定版本中执行此操作,而不是测试版。

最后,在完成所有操作后,您就可以开始编码了。在您的应用委托中附上此代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ...
  UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                              UIUserNotificationTypeBadge |
                                              UIUserNotificationTypeSound);
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                       categories:nil];
  [application registerUserNotificationSettings:settings];
  [application registerForRemoteNotifications];
  ...
}

请注意,以下代码演示了如何将用户添加到频道......这是Parse的默认代码,我实际建议稍微调整一下。他们进行了设置,以便每次启动应用程序时您的频道都会重置为全局。 我不确定你是否需要在将设备的令牌附加到后端之后再调用“registerUserNotificationSetting”方法。不要这样做会产生API请求......

- (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];
  // This is Parse's default code
  currentInstallation.channels = @[ @"global" ];
  // I would change it to something like this
  if (currentInstallation.channels.count == 0) {
    currentInstallation.channels = @[ @"global" ];
  }
  [currentInstallation saveInBackground];
}

最后,最后一段代码只是处理应用程序在前台收到通知时所执行的操作:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  [PFPush handlePush:userInfo];
}
祝你好运!

答案 1 :(得分:0)

发送通知时,只需将频道名称添加到通知数据中。

例如:

PFPush *push = [[PFPush alloc] init];
[push setChannel:@"my_channel"];
NSDictionary *data = @{@"alert":@"Hi there.",@"channel":@"my_channel"};
[push setData:data];
[push sendPushInBackground];

收到通知后,您可以从payload / userInfo中简单地获取频道:

NSString *channel = payload[@"channel"];