gpt4 book ai didi

ios - 使用 Parse 从一个用户向另一个用户发送推送通知

转载 作者:技术小花猫 更新时间:2023-10-29 10:51:31 24 4
gpt4 key购买 nike

我构建了一个类似于 Snapchat 的消息传递应用程序 - 一个用户可以向另一个用户发送图片。我正在尝试向应用程序添加推送通知,以便当消息从 UserA 发送到 UserB 时,UserB 会收到“来自 UserA 的新消息”的推送通知。

我已经研究了好几个小时了,我觉得我已经很接近了。

我正在尝试使用 Parse 发送推送通知。我希望它像这样工作:当 UserA 向 UserB 发送一条消息时,UserB 也会收到一条推送通知,上面写着“来自 UserA 的新消息”。我能够成功地使用 Parse 网站向使用该应用程序的设备发送推送通知,但我无法从应用程序内成功发送推送通知(当用户发送消息时)到接收用户的设备。

推送通知显然已成功发送,因为我的 Parse 帐户显示了我已发送的消息。但是,实际上没有消息到达预期的设备,并且推送通知列表显示每个推送通知有 0 个订阅者。

我可以单击其中一个来查看详细信息。

此外,我正在使用分发/生产配置文件和证书。

这是我在 UserA 向 UserB 发送消息后发送推送通知的代码 - message 对象是已上传到 Parse 的消息,messageRecipients 是用户消息被发送到:

// Send Push Notification to recipients

NSArray *messageRecipients = [message objectForKey:@"recipientIds"];

PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"owner" containedIn:messageRecipients];

PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setMessage:[NSString stringWithFormat: @"New Message from %@!", [PFUser currentUser].username]];
[push sendPushInBackground];

这是我的 AppDelegate.m 相关方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:@"[This is where my app Id is]"
clientKey:@"[This is where client Id is]"];
[self customizeUserInterface];
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];

return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[PFPush storeDeviceToken:deviceToken];
[PFPush subscribeToChannelInBackground:@""];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Did fail to register for push, %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[PFPush handlePush:userInfo];
}

我还在 Parse.com 论坛上提交了一个帖子:https://parse.com/questions/sending-a-push-notification-from-one-user-to-another-user

我是否遗漏或做错了什么?

编辑:我现在可以在我的 Parse 帐户中看到订阅者,但我实际上并没有在我的设备上收到推送通知。当我尝试从 Parse 网站发送推送通知测试时也是如此。 enter image description here

最佳答案

我的搜索一直回到这里,但这里没有任何东西真正为我拼写出来。所以这就是我的工作方式:

在我的 AppDelegate.m 中我有:

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

PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
//save the installation
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation[@"installationUser"] = [[PFUser currentUser]objectId];
// here we add a column to the installation table and store the current user’s ID
// this way we can target specific users later

// while we’re at it, this is a good place to reset our app’s badge count
// you have to do this locally as well as on the parse server by updating
// the PFInstallation object
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
// only update locally if the remote update succeeded so they always match
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(@"updated badge");
}
}];
}
} else {

[PFUser logOut];
// show the signup screen here....
}
}

在我发送推送的 viewController 中:

myViewController.h

@property (nonatomic, strong) NSMutableArray *recipients; // declare the array we'll use to store our recipients

myViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.recipients = [[NSMutableArray alloc] init]; // initialize the array we'll use to hold our recipients
}


// in another part of the code (not shown here) we set up a tableView with all of the current user's friends in it
// when the user taps a row in that tableView we add or remove the selected friend from our recipients list
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];

if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId]; // user selected a recipient, add them to the array
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId]; // user de-selected a recipient, remove them from the array
}

}




- (void)uploadMessage
{
UIImage *newImage = [self resizeImage:self.image toWidth:640.0f andHeight:960.0f];
NSData *fileData= UIImageJPEGRepresentation(newImage, 1.0);
NSString *fileName= @"image.jpg";;
NSString *fileType= @"image";

PFFile *file = [PFFile fileWithName:fileName data:fileData];

[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
PFObject *message = [PFObject objectWithClassName:@"Messages"];
[message setObject:file forKey:@"file"];
[message setObject:fileType forKey:@"fileType"];
[message setObject:self.recipients forKey:@"recipientIds"];
// self.recipients is an NSMutableArray of the objectIds for each
// user the message will go to
[message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
[message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
// Everything was successful! Reset UI… do other stuff
// Here’s where we will send the push
//set our options
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
@“Ne messages available!!”, @"alert",
@"Increment", @"badge",
nil];

// Now we’ll need to query all saved installations to find those of our recipients
// Create our Installation query using the self.recipients array we already have
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"installationUser" containedIn:self.recipients];

// Send push notification to our query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setData:data];
[push sendPushInBackground];
}
}];
}
}];
}

关于ios - 使用 Parse 从一个用户向另一个用户发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20626513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com