代码之家  ›  专栏  ›  技术社区  ›  dumbfingers

由于推送通知中的消息无法显示

  •  0
  • dumbfingers  · 技术社区  · 10 年前

    在坚果壳中,我有三个视图: Main View , List View (显示好友列表)和 Chat View (你聊天的地方)。

    推送通知在此应用程序中正确设置,因此我在接收推送通知负载方面没有问题,我的问题在于,在特定场景中,为什么接收到的消息似乎丢失了,并且没有出现在 聊天视图

    我有一个奇怪的问题,请继续阅读整个描述:

    因此,我在这里放了一些代码来演示如何处理推送通知:

    在AppDelegate.m中

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Extract the Sinch-specific payload from the Apple Remote Push Notification
        NSString* payload = [userInfo objectForKey:@"SIN"];
        [self initSinchClientWithUserId:userid];
    
        // Get previously initiated Sinch client
        id<SINNotificationResult> result = [_client relayRemotePushNotificationPayload:payload];
        if ([result isMessage]) {
            // Present alert notifying
            NSString *messageId = [[result messageResult] messageId];
            NSLog(@"Received messageid: %@", messageId);
    
            if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground ||
               [UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
                self.messageSenderId = [[result messageResult] senderId];
    
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedRemoteNotification" object:self userInfo:nil];
    
                NSLog(@"handle remote notification in modal");
    
            } else {
                self.messageSenderId = [[result messageResult] senderId];
                // this will launch a modal of private chat, so better to invoke this when running in background
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SetUnreadIcon" object:self userInfo:nil];
                NSLog(@"handle remote notification by marking icons");
    
            }
    
        } else if (![result isValid]) {
            // Handle error
        }
        NSLog(@"Received Payload: %@", payload);
    }
    

    在主视图中,控制柄 ReceivedRemoteNotification SetUnreadIcon

    -(void) viewDidLoad {
    
    [super viewDidLoad];
    ....
    // setup a local notification handler
        NSNotificationCenter *notifCentre = [NSNotificationCenter defaultCenter];
        [notifCentre addObserver:self selector:@selector(invokeRemoteNotifications:) name:@"ReceivedRemoteNotification" object:nil];
        [notifCentre addObserver:self selector:@selector(setUnreadIcon:) name:@"SetUnreadIcon" object:nil];
    ....
    }
    
    - (void)invokeRemoteNotifications: (NSNotification *) notification {
        shouldDismiss = NO;
        [self invokeNotifications];
    }
    
    - (void)invokeNotifications {
        [self performSegueWithIdentifier:@"modalToChat" sender:self];
    }
    
    - (void)setUnreadIcon: (NSNotification *) notification {
        AudioServicesPlaySystemSound (1300);
        shouldDismiss = YES;
        [self.rightbutton setCustomView:notifyButton];
    }
    

    用户点击时 rightbutton 在MainView中,将调用 ListView .

    在ChatView中,它实现 SINMessageClientDelegate 委托方法( See Documentation ),即

    – messageClient:didReceiveIncomingMessage:
    – messageSent:recipientId:
    – messageDelivered:
    – messageFailed:info:
    – message:shouldSendPushNotifications:
    

    工作场景

    假设用户 Alice 将消息发送到 Bob . 上下快速移动 通过推送通知收到此消息。所以 上下快速移动 打开并点击通知,应用程序将显示并自动显示 ChatView .

    在这种情况下,推送通知中的消息可以显示在 聊天视图

    非工作场景

    假设用户 爱丽丝 将消息发送到 上下快速移动 再次,这次 上下快速移动 在应用程序处于前台时收到通知。因此,将执行此方法:

    - (void)setUnreadIcon: (NSNotification *) notification {
            AudioServicesPlaySystemSound (1300);
            shouldDismiss = YES;
            [self.rightbutton setCustomView:notifyButton];
        }
    

    然后 上下快速移动 将轻敲 右按钮 打电话来 列表视图 ,找到爱丽丝并打开对话。 上下快速移动 将发现刚刚从 爱丽丝 未在中显示 聊天视图

    问题

    在Sinch SDK中,似乎没有手动检索消息的方法,即使是 messageId 在接收推送通知时检索。我说得对吗?

    在“不工作”的情况下,为什么消息会丢失?如果sinch客户端负责转发消息,那么有什么理由让它丢弃消息?

    仍然在“不工作”的情况下,我如何保存消息,然后稍后显示它?我必须实施 SINMessageClient委托 在别处

    谢谢

    1 回复  |  直到 10 年前
        1
  •  3
  •   VMai    10 年前

    关于您的第一个问题,无法以您描述的方式查询特定消息。

    您的“不工作”案例的问题很可能是,当Sinch SDK获取即时消息并即将通知消息客户端委托时 Chat View 尚未实例化/分配为委托,因此委托“错过”了消息。

    我建议您使用非ViewController组件实现 SINMessageClientDelegate ,并使该组件具有独立于视图控制器的生命周期。即,在应用程序启动时创建充当代理的组件,并使其保持活动状态。然后,您可以确保始终收到所有 onIncomingMessage: ,您还可以在那里放置用于持久化消息的逻辑。