Parse.com回复推送通知
问题描述:
我想通过推送通知在我的应用中设置响应。Parse.com回复推送通知
用户A向用户B发送消息。
用户B将应用程序(通过推送)打开到新页面。
用户B向用户A发送响应(作为推送)。
现在我可以打开页面,但我不知道如何获取我需要的数据。
这是我从解析得到第一推:
userInfo: {
aps = {
alert = "demo says HELLO WORLD";
};
在这种情况下,演示的是,发送的第一个推用户的用户名。我想知道这一点,以便用户B的应用知道发送回复的人。
这里是我的推送代码:
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setMessage:[NSString stringWithFormat:@"%@ says HELLO WORLD", [PFUser currentUser].username]];
[push sendPushInBackground];
答
你将不得不从AppDelegate中处理这个问题。有两种方法可以从推送通知中接收数据。
//Receive Push Notification when the app is active in foreground or background
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
if(userInfo){
//TODO: Handle the userInfo here
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Get the push notification when app is not open
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif){
[self handleRemoteNotification:application userInfo:remoteNotif];
}
return YES;
}
-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{
if(userInfo){
//TODO: Handle the userInfo here
}
}
更新如下答案: -
在这种情况下,我认为setData
,而是如果setMessage
你应该使用。
NSString * message =[NSString stringWithFormat:@"%@ says HELLO WORLD", [PFUser currentUser].username];
NSString * userID = @"userid"; //TODO: set Your userID here
NSMutableDictionary * dataDict = [[NSMutableDictionary alloc]init];
[dataDict setObject:message forKey:@"message"];
[dataDict setObject:userID forKey:@"userID"];
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setData:dataDict];
[push sendPushInBackground];
答
通知有效负载可以包含字典条目以向接收应用程序提供附加数据。这在Apple Local and Push Notification Programming Guide中有记录。
当您生成在解析您的Push消息,您可以添加发送用户的objectID,让你的有效载荷是这样
userInfo: {
aps = {
alert = "demo says HELLO WORLD";
},
sendingUserObject:142Xyd23
};
您的云代码会是这样的 -
-var pushMsg=user.get("username")+" says HELLO WORLD";
var pushData={alert: pushMsg, sendingUserObject: user.id};
var pushMap= {};
pushMap["data"]=pushData;
var deviceQuery=new Parse.Query("installations");
deviceQuery.equalTo("currentUser",destination);
deviceQuery.exists("deviceToken");
pushMap["where"]=deviceQuery;
Parse.Push.send(pushMap);
然后,当您收到您的应用程序通知您可以从userInfo
字典检索发送对象ID
没错。我已经设置好了,但是我上面展示的是我在userInfo中获得的所有内容。我如何获得发送推送的用户的objectId? – 2014-08-29 04:08:24
请参阅上面的更新。 – Ricky 2014-09-02 02:07:33