简体   繁体   中英

Parse Push Notification Get Notification Channel - iOS

i recently set up push notifications for my app using Parse. I have been able to get the notifications and the data sent but now i want to get the channel to which the notification has been sent. Is there any way to do so in ios?

Have you checked this guide? It's really helpful.

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

Here's a quick summary. Create development and production certificates and then attach them to your developer account for the app that you want to be able to send pushes to. After attaching these certificates redownload them, change them to .p12 files w Keychain Access and then upload them to Parse.com. In Xcode make sure to go into your account via preferences and refresh it so you'll have an updated provisioning profile. I'd do this in the stable version of Xcode and not the beta.

Finally after doing all that you can start coding. In your app delegate attach this code:

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

Please note that the following code demonstrates how to add a user to a channel... This is Parse's default code and I actually suggest tweaking it a bit. They set it up so that your channels will be reset to global every time the application is launched. I'm not sure that you even need to invoke the method "registerUserNotificationSetting" at all after attaching the device's token to the backend. Take not this method will generate an API request...

- (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];
}

Finally the last bit of code just deals with what the application does if it receives a notification while in the foreground:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  [PFPush handlePush:userInfo];
}

Good luck!

When you send the notification just add the channel name to the notifications data.

For example:

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

When you receive the notification you can simple get the channel from the payload/userInfo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM