简体   繁体   中英

How do I receive a push notification to one app from another?

I have followed the Quick Start Guide for parse: https://parse.com/apps/quickstart#parse_data/mobile/ios/native/existing

I'm using this code to save a PFObject :

PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
testObject[@"Bookmarks"] = @"Restore Successful";
[testObject saveInBackground];

Below is what it looks like when my PFObject is saved to my Parse backend: 在此处输入图片说明

So it's safe to say that saving the object is good to go but I need this process:

  1. App A saves PFObject to Parse backend (i'm already doing this)
  2. Parse backend sends push notification to App B (how do i automatically do this?)
  3. App B receives push notification saying what was saved(how do i do this?)

Is there any way to do this? I've tried Cloud Code but it's really confusing. I've literally tried setting it up, like, 6 times :( I know how to setup push notifications but I don't know how to get parse to automatically send a push notification when App A saves a new PFObject .

I just need to be notified when my Parse backend receives a new PFObject ,

You shouldn't look at it like "one app to another" instead, you should focus on, from one circumstance to another, because this can limit your future implementations, unless you are strictly coding for admin or simplistic purposes, just do what's best for your project. Parse pushes currently support up to 6 different push certificates using the same Parse.com applicationId & clientKey . Essentially, all you need to do is create an individual push certificate for each app, just like you did the for the first one, and upload it to your Push Certificates in your Parse.com settings. This is not device/OS specific, and will not work until you add these to your backend console. After that is complete, enable the Client Push Enabled setting and then you simply just need to target your push notifications. Since you haven't included 'User' as a priority in your question, simply take advantage of a parameter for targeting application names/ids. See here . In other words, target your push notification to use the applicationId column or appName column in your Installation class

Sending a push to a user:

PFQuery * pushQuery = [PFInstallation query];
PFUser * userReceivingPush;
[pushQuery whereKey:@"owner" equalTo:userReceivingPush];

NSString * alert = [NSString stringWithFormat:@"MESSAGE_FROM %@", [PFUser currentUser].username];
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys: alert, @"alert", @"default", @"sound",  @"Increment", @"badge", nil];
[PFPush sendPushDataToQueryInBackground:pushQuery withData:data block:^(BOOL succeeded, NSError *error) {
    if (!error) {
    } else {

    }
}];

Responding if app isn't running:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  . . .
  // Extract the notification data
  NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

  // Create a pointer to the Photo object
  NSString *photoId = [notificationPayload objectForKey:@"p"];
  PFObject *targetPhoto = [PFObject objectWithoutDataWithClassName:@"Photo"   objectId:photoId];

  // Fetch photo object
  [targetPhoto fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    // Show photo view controller
    if (!error) {
      PhotoVC *viewController = [[PhotoVC alloc] initWithPhoto:object];
      [self.navController pushViewController:viewController animated:YES];
    }
  }];
}

If your app is already running when the notification is received, the data is made available in the application:didReceiveRemoteNotification:fetchCompletionHandler: method through the userInfo dictionary:

- (void)application:(UIApplication *)application
  didReceiveRemoteNotification:(NSDictionary *)userInfo  fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
  // Create empty photo object
  NSString *photoId = [userInfo objectForKey:@"p"];
  PFObject *targetPhoto = [PFObject objectWithoutDataWithClassName:@"Photo"   objectId:photoId];

  // Fetch photo object
  [targetPhoto fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    // Show photo view controller
    if (error) {
      handler(UIBackgroundFetchResultFailed);
    } else if ([PFUser currentUser]) {
      PhotoVC *viewController = [[PhotoVC alloc] initWithPhoto:object];
      [self.navController pushViewController:viewController animated:YES];
      handler(UIBackgroundFetchResultNewData);
    } else {
      handler(UIBackgroundModeNoData);
    }
  }];
}

read more here: https://parse.com/docs/ios/guide

another push:

NSDictionary *data = @{
  @"alert" : @"The Mets scored! The game is now tied 1-1!",
  @"badge" : @"Increment",
  @"sounds" : @"cheering.caf"
};
PFPush *push = [[PFPush alloc] init];
[push setChannels:@[ @"Mets" ]];
[push setData:data];
[push sendPushInBackground];

Push Options:

Customizing your Notifications

If you want to send more than just a message, you will need to use an NSDictionary to package all of the data. There are some reserved fields that have a special meaning.

**alert**: the notification's message.
**badge**: (iOS/OS X only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.
**sound**: (iOS/OS X only) the name of a sound file in the application bundle.
content-available: (iOS only) If you are a writing a Newsstand app, or an app using the Remote Notification Background Mode introduced in iOS7 (a.k.a. "Background Push"), set this value to 1 to trigger a background download.
**category**: (iOS only) the identifier of th UIUserNotificationCategory for this push notification.
**uri**: (Android only) an optional field that contains a URI. When the notification is opened, an Activity associate with opening the URI is launched.
**title**: (Android, Windows 8, and Windows Phone 8 only) the value displayed in the Android system tray or Windows toast notification.

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