推送通知(Parse / Cloud Code)不显示评论或任何喜欢的用户名

时间:2015-04-18 23:46:03

标签: javascript objective-c ios8 push-notification cloud-code

我第一次使用Parse和Cloud Code来更新使用Parse和Objective-C for iOS 8构建的照片共享应用程序。我已按照Parse的说明设置推送通知并且已成功启用在照片被评论时收到通知,但它只提供“某人已对您的照片发表评论”的“其他”声明(而不是提供用户的姓名)。除此之外,“喜欢”通知根本没有被推送。

这是我在Parse的AnyPic教程之后建模的我的云代码(JavaScript)。我还有一个安装文件和一个指向这个和安装的“主”文件。我在这里缺少什么?

Parse.Cloud.beforeSave('Activity', function(request, response) {
  var currentUser = request.user;
  var objectUser = request.object.get('fromUser');

  if(!currentUser || !objectUser) {
    response.error('An Activity should have a valid fromUser.');
  } else if (currentUser.id === objectUser.id) {
    response.success();
  } else {
    response.error('Cannot set fromUser on Activity to a user other than the current user.');
  }
});

Parse.Cloud.afterSave('Activity', function(request) {
  // Only sends push notifications for new activities.
  if (request.object.existed()) {
    return;
  }

  var toUser = request.object.get("toUser");
  if (!toUser) {
    throw "Undefined toUser. Skipping push for Activity " + request.object.get('type') + " : " + request.object.id;
    return;
  }

  var query = new Parse.Query(Parse.Installation);
  query.equalTo('user', toUser);

  Parse.Push.send({
    where: query, // Set our Installation query.
    data: alertPayload(request)
  }).then(function() {
    // Push was successful
    console.log('Sent push.');
  }, function(error) {
    throw "Push Error " + error.code + " : " + error.message;
  });
});

var alertMessage = function(request) {
  var message = "";

  if (request.object.get("type") === "comment") {
    if (request.user.get('displayName')) {
      message = request.user.get('displayName') + ': ' + request.object.get('content').trim();
    } else {
      message = "Someone left a comment on your photo.";
    }
  } else if (request.object.get("type") === "like") {
    if (request.user.get('displayName')) {
      message = request.user.get('displayName') + ' likes your photo.';
    } else {
      message = 'Someone likes your photo.';
    }
  }

  // Trim our message to 140 characters.
  if (message.length > 140) {
    message = message.substring(0, 140);
  }

  return message;
}

var alertPayload = function(request) {
  var payload = {};

  if (request.object.get("type") === "comment") {
    return {
      alert: alertMessage(request), // Sets the alert message.
      badge: 'Increment', // Increments the target device's badge count.
      // The following keys help Cosplace to load the photo corresponding to this push notification.
      p: 'a', // Payload Type: Activity
      t: 'c', // Activity Type: Comment
      fu: request.object.get('fromUser').id, // From User
      pid: request.object.id // Photo Id
    };
  } else if (request.object.get("type") === "like") {
    return {
      alert: alertMessage(request), // Sets the alert message.
      // The following keys help Cosplace to load the photo corresponding to this push notification.
      p: 'a', // Payload Type: Activity
      t: 'l', // Activity Type: Like
      fu: request.object.get('fromUser').id, // From User
      pid: request.object.id // Photo Id
    };
  }
}

谢谢!

编辑:这是AppDelagate.m文件中的Objective-C代码(我拿出了Parse ID):

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [Parse setApplicationId: @""

                      clientKey:@""];

        [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

        // Reset application icon badge number when app is launched.
        if (application.applicationIconBadgeNumber != 0) {
            application.applicationIconBadgeNumber = 0;
            [[PFInstallation currentInstallation] saveInBackground];
        }

        // Registers the current device for push notifications.
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];

        if (application.applicationState != UIApplicationStateBackground) {
            // Track an app open here if we launch with a push, unless
            // "content_available" was used to trigger a background push (introduced
            // in iOS 7). In that case, we skip tracking here to avoid double
            // counting the app-open.
            BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)];
            BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
            BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
            if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
                [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
            }
        }
        // If the registration is successful, this callback method will be executed. This method informs Parse about this new device.
        - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
            // Reset application icon badge number when app is launched.
            if (application.applicationIconBadgeNumber != 0) {
                application.applicationIconBadgeNumber = 0;

            // Store the deviceToken in the current installation and save it to Parse.
            PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        //    [PFInstallation.currentInstallation setObject:PFUser.currentUser forKey:@"user"];
            [currentInstallation setDeviceTokenFromData:deviceToken];
            // Associate the device with a user
            PFInstallation *installation = [PFInstallation currentInstallation];
            installation[@"user"] = [PFUser currentUser];
            [installation saveInBackground];
            [currentInstallation saveInBackground];

            // Create Parse Installation query
            PFQuery *pushQuery = [PFInstallation query];
            [pushQuery whereKey:@"deviceType" equalTo:@"ios"];

            }
        }
            // So when logged out notifications stop being received.
        - (void)logOut {
            // Unsubscribe from push notifications by removing the user association from the current installation.
            [[PFInstallation currentInstallation] removeObjectForKey:@"user"];
            [[PFInstallation currentInstallation] saveInBackground];
        }

        - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
            [PFPush handlePush:userInfo];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AppDelegateApplicationDidReceiveRemoteNotification" object:nil userInfo:userInfo];
        }

    - (void)applicationDidBecomeActive:(UIApplication *)application {

        // Clear badge and update installation for auto-incrementing badges.
        if (application.applicationIconBadgeNumber != 0) {
            application.applicationIconBadgeNumber = 0;
            [[PFInstallation currentInstallation] saveInBackground];
        }
}

0 个答案:

没有答案