Javascript 推送通知(解析/云代码)不显示评论或任何类似内容的用户名
我第一次使用Parse和Cloud代码来更新一个照片共享应用程序,该应用程序是用Parse和Objective-C为iOS 8构建的。我按照Parse的说明设置推送通知,并成功地在照片被评论时接收到通知,但它只传递“其他人”语句,表示“有人对您的照片发表了评论”(而不是提供用户名)。除此之外,“喜欢”的通知根本不会被推送 这是我的云代码(JavaScript),我是根据Parse的AnyPic教程建模的。我还有一个安装文件和一个指向此文件和安装的“主”文件。我错过了什么Javascript 推送通知(解析/云代码)不显示评论或任何类似内容的用户名,javascript,objective-c,ios8,push-notification,parse-cloud-code,Javascript,Objective C,Ios8,Push Notification,Parse Cloud Code,我第一次使用Parse和Cloud代码来更新一个照片共享应用程序,该应用程序是用Parse和Objective-C为iOS 8构建的。我按照Parse的说明设置推送通知,并成功地在照片被评论时接收到通知,但它只传递“其他人”语句,表示“有人对您的照片发表了评论”(而不是提供用户名)。除此之外,“喜欢”的通知根本不会被推送 这是我的云代码(JavaScript),我是根据Parse的AnyPic教程建模的。我还有一个安装文件和一个指向此文件和安装的“主”文件。我错过了什么 Parse.Cloud.
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代码(我取出了解析ID):
您可能无法通过
request.user
获得完整的用户对象。在发送push.Hm之前,您可能需要查询完整的用户详细信息。谢谢,我得看看怎么做。我认为这就是我已经在做的,所以我意识到解析云代码的JavaScript可能需要参考。这就是为记录照片评论而设置解析类的地方。“fromUserName”是发表评论的人的用户名,&“fromUser”是他们的用户ID(指针)。我认为request.user.get()应该在推送消息中调用“fromUserName”之前调用“fromUser”来获取用户ID,但这并没有改变任何事情。因为我得到了评论的“else”消息,所以它必须是“if(request.user.get('fromUser'))”,这是不对的。我明白了。其中我有“displayName”,我需要使用“fromUser”,从我的Parse class表。我还需要将消息更改为使用“request.object.get(fromUserName)”而不是“request.user.get(displayName)”
- (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];
}
}