How to handle received push notification from pars

2019-08-31 04:59发布

问题:

Using Parse, my push notification working perfectly. my app is an RSS news feed, and from time to time I'm sending Push notification, my problem is that I don't know how to handle the push notification when the user receive it. I've got all my RSS sources listed in a plist file, for example how my plist file looks:

rss_sources

↓ 
01
  ↓
  url http://www.newyorkNews.com/rss.xml
  title: new york News
↓
02
  ↓
  url http://www.harlemNews.com/rss.xml
  title: harlem news

what i'm trying to do is, to check if the title is equal to the beginning of the push notification (since I'm the one who sending the push, i'll write the exact title), and if it does then it will go to some index.row that i set in code. is it even possible what I've offered here? if theres another way of doing it I will be glad to hear the solution, or some pattern of code that similar to my situation so I can get inspire from it.

回答1:

All you have to do is set a generic key to your payload which in your case looks like title. So when you send a push (as data/payload/json), when user receives one you cross reference the valueForKey:

As always, I highly encourage you try things out yourself because that's how you learn. And I always direct Parse users to their documentation because they are extremely well-documented. Almost too documented if that's a thing. However if you get stuck here is a working example:

Construct a push with payload:

NSDictionary *data = @{
@"alert" : @"some generic message here",
@"badge" : @"Increment",
@"sounds" : @"default",
@"title" : @"NY Times" //this is whatever you want
};

//schedule the push with some options. This isn't a mandatory set up, just an example. You can do a lot with PFPushes

PFPush *push = [[PFPush alloc] init];
[push setChannels:@[ @"subscribed" ]];
[push setData:data];
[push sendPushInBackground];

Now all you have to is see if the value in the payload for the key title matches your needs :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  . . .
  // Extract the notification data from payload

 NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

 NSString *newsType = [notificationPayload valueForKey:@"title"];

  // perform segue or tab bar selectedIndex or whatever you want after checking if user is launching from notification :

if (notificationPayload) {
    //check it title has your string 
    if ([newsType isEqualToString:@"NY Times"]) {
         //do whatever here 
    } else {

    }
 }
}

references - please use these liberally, they've done great with providing this up-to-date resource to us

Parse iOS Push : https://parse.com/docs/push_guide#top/iOS

Parse SDK https://parse.com/docs/ios/api/


Push Notification from Parse console :

{
"aps" : {
    "alert" : "New NY Time Article",
    "badge" : 1,
    "sound" : "default",
    "title" : "NY Times"
        }
}

For reference this will get you started : https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW15