Подтвердить что ты не робот

Отправка Push-уведомления от одного пользователя другому пользователю с помощью Parse

Я создал приложение для обмена сообщениями, которое похоже на Snapchat - один пользователь может отправлять изображения других пользователей. Я пытаюсь добавить push-уведомления в приложение, так что, когда сообщение отправляется из UserA в UserB, UserB получает push-уведомление "Новое сообщение от пользователяA".

Я изучаю это часами, и я чувствую, что очень близко.

Я пытаюсь использовать Parse для отправки push-уведомлений. Я бы хотел, чтобы он работал следующим образом: Когда UserA отправляет UserB сообщение, UserB также отправляет push-уведомление, в котором говорится "Новое сообщение от UserA". Я успешно смог использовать веб-сайт Parse для отправки push-уведомления устройствам с помощью приложения, но НЕ удастся успешно отправить push-уведомление из приложения (когда пользователь отправляет сообщение) на принимающее пользовательское устройство.

Push-уведомления, по-видимому, отправляются успешно, так как моя учетная запись Parse показывает отправленные мной сообщения. Однако на самом деле сообщения не доходят до предполагаемого устройства, а список push-уведомлений показывает 0 подписчиков для каждого push-уведомления.

И я могу нажать на один из них, чтобы увидеть подробности.

Кроме того, я пользуюсь профилем и сертификатом распределения/производства.

Здесь код, который я использую, отправляет push-уведомление после того, как 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/info/sending-a-push-notification-from-one-user-to-another-user

Есть ли что-то, что мне не хватает или что-то не так?

EDIT: теперь я могу видеть подписчиков в моей учетной записи Parse, но на самом деле я не получаю push-уведомления на своем устройстве. То же самое происходит, когда я пытаюсь отправить тестовый запрос push с веб-сайта Parse. enter image description here

4b9b3361

Ответ 1

Мои поиски продолжали возвращаться сюда, но здесь ничего не объяснялось. Итак, вот как я получил свою работу:

В моем 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, где я отправляю push, у меня есть:

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 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];
                }
            }];
        }
    }];
}

Ответ 2

Думаю, у меня есть ответ. Я столкнулся с той же проблемой, и я просто решил ее.

Когда вы устанавливаете значение для владельца, вы должны использовать "[PFUser currentUser].objectId" вместо [PFUser currentUser]. Последний дает вам указатель на владельца, но нам нужна строка для установки Push-запроса в этой ситуации.

Когда мы сначала устанавливаем владельца на Installation, мы должны установить objectId как строку для владельца вместо просто [PFUser currentUser], как показано ниже.

[currentInstallation setObject:[PFUser currentUser].objectId forKey:@"owner"];

И позже мы могли бы установить владельца (строку) в наш pushQuery.

Ответ 3

Вы пытались сохранить владельца в процессе установки. то есть.

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation setObject:[PFUser currentUser] forKey:@"owner"];
    [currentInstallation saveInBackground];
}

От: нажмите здесь

Ответ 4

Учитывая то, что вы опубликовали до сих пор, я не знаю, почему ваш код не работает для вас. Кажется, что либо ваша таблица установки не содержит столбец "владелец", либо содержит столбец "владелец" со значениями/типами, отличными от тех, что содержатся в вашем массиве [message objectForKey:@"recipientIds"]; но на всякий случай вам нужно/нужно попробовать резервный метод, здесь другой способ отправить push-уведомления с одного устройства на другое с помощью Parse:

Сначала подпишитесь на пользователя/группу пользователей, получающих push на свой собственный уникальный канал (в дополнение к основному каналу), например

[PFPush subscribeToChannelInBackground:taylors_channel];

Затем из устройства, отправляющего push, установите канал в канал получателя:

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

Кроме того, вы также можете установить один и тот же канал для нескольких одноранговых узлов или отправить сообщение нескольким каналам одновременно с помощью [push setChannels:channel_array];

Ответ 5

Чтобы узнать о ответе djshiow, как только вы сохранили текущую установку как таковую.

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

NSString * alert = [NSString stringWithFormat:@"You have a new 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 {
                }
            }];