XMPP推送通知邮件中造成问题(延迟+重复) [英] XMPP push notifications causing problems (delay + duplications) in messages

查看:345
本文介绍了XMPP推送通知邮件中造成问题(延迟+重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XMPP推送通知,导致邮件的问题(延迟+重复)。

我已经成功地创建使用聊天应用程序XMPP + Ejabberd。

无推送通知:

无论是单和群聊消息都完美的工作。

通过推送通知:

有时候一切正常perfectly.Notifications被触发和消息有任何延迟或重复接收。

轻微伤通知被触发(而在后台的应用程序),但消息被完全接收。

有时,通知被触发,但消息被延迟和重复获得。

配置上断绝一切一边劝correctly.They通过确保每个会话一个持久性资源连接,使用空格永葆当连接丢失,只是同一资源重新绑定使连接稳定,以解决您的问题。

我有流管理,xmppStream.enableBackgroundingOnSocket和App在基于IP的服务后台模式提供了语音。

当用户注销或应用程序被终止我的拆解流和发送不可用presence。

下面是我的code为XMPP流推送通知和连接/断开。

我拉出我的头发超过this.if你们有什么想法,请让我知道。

感谢。

 的#pragma马克 - 连接/断开连接 - (BOOL)连接{如果(!_xmppStream){
    的NSLog(@设置流);
    [个体经营setupStream]
}如果(![_ xmppStream isDisconnected]){
    返回YES;
}* NSString的JABBERID = [[NSUserDefaults的standardUserDefaults] stringForKey:@用户ID];
* NSString的MYPASSWORD = [[NSUserDefaults的standardUserDefaults] stringForKey:@的userPassword]​​;
如果(JABBERID == ||零== MYPASSWORD无){
    返回NO;
}
[_xmppStream setMyJID:[XMPPJID jidWithString:JABBERID]];
_password = MYPASSWORD;NSError *误差=零;如果([_ xmppStream connectWithTimeout:XMPPStreamTimeoutNone错误:&放大器;错误]!){    UIAlertView中*警报= [[UIAlertView中页头] initWithTitle:@错误的消息:[NSString的stringWithFormat:@!无法连接到服务器%@,[错误localizedDescription]代表:无cancelButtonTitle:@确定otherButtonTitles:零];
    [警报显示]
    返回NO;
}
 返回YES;
 } - (无效){断开[个体经营goOffline]
[个体经营teardownStream]} - (无效)xmppStreamDidAuthenticate:(XMPPStream *){发件人  [个体经营goOnline]//流管理NSXMLElement *启用= [NSXMLElement elementWithName:@使能的xmlns:@金塔:XMPP:SM:3];
[启用addAttributeWithName:@简历stringValue的:@真];
[_xsm.xmppStream sendElement:启用];//推
[个体经营configurePushNotifications]
//} - (无效){configurePushNotifications* NSString的JABBERID = [[NSUserDefaults的standardUserDefaults] stringForKey:@用户ID];NSXMLElement *智商= [NSXMLElement elementWithName:@智商];
[智商addAttributeWithName:@类型stringValue的:@设置];
[智商addAttributeWithName:@标识stringValue的:idString];NSXMLElement *推= [NSXMLElement elementWithName:@推的xmlns:@P1:推];NSXMLElement *存活= [NSXMLElement elementWithName:@保活];
[存活addAttributeWithName:@MAXintegerValue:30];NSXMLElement *会话= [NSXMLElement elementWithName:@会话];
[会议addAttributeWithName:@持续时间integerValue:60];NSXMLElement *体= [NSXMLElement elementWithName:@身体];
[机构addAttributeWithName:@发送stringValue的:@所有];
[机构addAttributeWithName:@群聊stringValue的:@真];
[机构addAttributeWithName:@由stringValue的:JABBERID];NSXMLElement *状态= [NSXMLElement elementWithName:@状态];
[状态addAttributeWithName:@型stringValue的:[的NSString stringWithFormat:@从%@新的消息,JABBERID]];NSXMLElement *离线= [NSXMLElement elementWithName:@离线stringValue的:@真];[推的addChild:保活];
[推的addChild:会议];
[推的addChild:机构];
[推的addChild:状态]。
[推的addChild:下线];NSXMLElement *通知= [NSXMLElement elementWithName:@通知];
[通知的addChild:[NSXMLElement elementWithName:@类型stringValue的:@applepush]];
[通知的addChild:[NSXMLElement elementWithName:@标识stringValue的:_userDeviceToken]];[推的addChild:通知]NSXMLElement *的appid = [NSXMLElement elementWithName:@的appidstringValue的:@的appid];[推的addChild:APPID];[智商的addChild:推动];[自xmppStream] sendElement:智商];
 } - (无效){setupStream_xmppStream = [[XMPPStream的alloc]初始化];
_xmppStream.hostName = kHostName;
_xmppStream.hostPort = kHostPort;
_xmppStream.enableBackgroundingOnSocket = YES;
[_xmppStream addDelegate:自delegateQueue:dispatch_get_main_queue()];// XMP preconnect_xmp preconnect = [[XMP preconnect的alloc]初始化];
[_xmp preconnect激活:_xmppStream];//流管理_xsm = [[XMPPStreamManagement的alloc]初始化];
[_xsm enableStreamManagementWithResumption:YES maxTimeout:0];
[_xsm激活:_xmppStream];//最近活动_xmppLastActivity = [[XMPPLastActivity页头] initWithDispatchQueue:dispatch_get_main_queue()];
[_xmppLastActivity addDelegate:自delegateQueue:dispatch_get_main_queue()];
[_xmppLastActivity激活:_xmppStream]; }  - (无效){goOnline
XMPP presence * presence = [XMPP presence presence]。
[自xmppStream] sendElement:presence]。
 }  - (无效){goOffline
XMPP presence * presence = [XMPP presence presenceWithType:@不可用];
[自xmppStream] sendElement:presence]。
} - (无效){teardownStream[_xmppStream断开][_xmppStream removeDelegate:个体经营];
[_xmp preconnect removeDelegate:个体经营];[_xmppLastActivity removeDelegate:个体经营];[_xmp preconnect关闭]
_xmppStream =零;
_xmp preconnect =零;
_xmppLastActivity =零;  }


解决方案

您需要确保要传递的资源当您连接到ejabberd。资源应在首次安装应用程序随机生成,并在随后的登录时,你应该总是使用相同的资源。否则,你是在服务器上的每个新的登录创建新的长期运行的独立会议,并导致邮件被路由到所有未决的会话。当这些过期它们再次路由等。

在XMPP,资源基本上是该装置的标识符。你需要与形式的字符串来生成JID进行登录为user @ domain /资源

XMPP push notifications causing problems (delay + duplications) in messages.

I have successfully created a chat application using XMPP + Ejabberd.

Without Push Notifications:

Both single and group chat messages are working perfectly.

With Push Notifications:

Sometimes everything works perfectly.Notifications are triggered and messages are received with out any delay or duplications.

Sometimes no notifications are triggered (while app in background) but messages are received perfectly.

Sometimes notifications are triggered but messages are received with delay and duplications.

Everything on the sever side is configured correctly.They advised to fix your issues by making sure each session connects with one persistent resource, making connection stable using whitespace keep alive and when connection is lost just rebinding with same resource.

I have stream management,xmppStream.enableBackgroundingOnSocket and App provides Voice over IP services background mode enabled.

When user logs out or app is terminated i teardown the stream and send an unavailable presence.

Below is my code for xmpp stream push notifications and connect/disconnect.

I am pulling out my hair over this.if you guys have any idea please let me know.

Thanks.

#pragma mark - Connect/Disconnect

- (BOOL)connect {

if (!_xmppStream) {
    NSLog(@"Setting up Stream");
    [self setupStream];
}

if (![_xmppStream isDisconnected]) {
    return YES;
}

NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"];


if (jabberID == nil || myPassword == nil) {
    return NO;
}
[_xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
_password = myPassword;

NSError *error = nil;

if (![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]){

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Can't connect to server! %@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    return NO;
}
 return YES;
 }

- (void)disconnect {

[self goOffline];
[self teardownStream];

}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {

  [self goOnline];

//Stream Management

NSXMLElement *enable = [NSXMLElement elementWithName:@"enable" xmlns:@"urn:xmpp:sm:3"];
[enable addAttributeWithName:@"resume" stringValue:@"true"];
[_xsm.xmppStream sendElement:enable];

//Push
[self configurePushNotifications];
//

}

-(void)configurePushNotifications{

NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];

NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addAttributeWithName:@"id" stringValue:idString];

NSXMLElement *push = [NSXMLElement elementWithName:@"push" xmlns:@"p1:push"];

NSXMLElement *keepalive = [NSXMLElement elementWithName:@"keepalive"];
[keepalive addAttributeWithName:@"max" integerValue:30];

NSXMLElement *session = [NSXMLElement elementWithName:@"session"];
[session addAttributeWithName:@"duration" integerValue:60];

NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body addAttributeWithName:@"send" stringValue:@"all"];
[body addAttributeWithName:@"groupchat" stringValue:@"true"];
[body addAttributeWithName:@"from" stringValue:jabberID];

NSXMLElement *status = [NSXMLElement elementWithName:@"status"];
[status addAttributeWithName:@"type" stringValue:[NSString stringWithFormat:@"New message from %@",jabberID]];

NSXMLElement *offline = [NSXMLElement elementWithName:@"offline" stringValue:@"true"];

[push addChild:keepalive];
[push addChild:session];
[push addChild:body];
[push addChild:status];
[push addChild:offline];

NSXMLElement *notification = [NSXMLElement elementWithName:@"notification"];
[notification addChild:[NSXMLElement elementWithName:@"type" stringValue:@"applepush"]];
[notification addChild:[NSXMLElement elementWithName:@"id" stringValue:_userDeviceToken]];

[push addChild:notification];

NSXMLElement *appid = [NSXMLElement elementWithName:@"appid" stringValue:@"appid"];

[push addChild:appid];

[iq addChild:push];

[[self xmppStream] sendElement:iq];


 }

- (void)setupStream {

_xmppStream = [[XMPPStream alloc] init];
_xmppStream.hostName = kHostName;
_xmppStream.hostPort = kHostPort;
_xmppStream.enableBackgroundingOnSocket = YES;
[_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

//XMPPReconnect

_xmppReconnect = [[XMPPReconnect alloc] init];
[_xmppReconnect activate:_xmppStream];

//Stream Management

_xsm = [[XMPPStreamManagement alloc] init];
[_xsm enableStreamManagementWithResumption:YES maxTimeout:0];
[_xsm activate:_xmppStream];

//Last Activity

_xmppLastActivity = [[XMPPLastActivity alloc] initWithDispatchQueue:dispatch_get_main_queue()];
[_xmppLastActivity addDelegate:self delegateQueue:dispatch_get_main_queue()];
[_xmppLastActivity activate:_xmppStream];

 }

 - (void)goOnline {
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
 }

 - (void)goOffline {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[[self xmppStream] sendElement:presence];
}

- (void)teardownStream {

[_xmppStream disconnect];

[_xmppStream removeDelegate:self];
[_xmppReconnect removeDelegate:self];

[_xmppLastActivity removeDelegate:self];

[_xmppReconnect deactivate];


_xmppStream = nil;
_xmppReconnect = nil;
_xmppLastActivity = nil;

  }

解决方案

You need to make sure that you are passing the resource when you connect to ejabberd. The resource should be randomly generated on first app install and on subsequent login, you should always use the same resource. Otherwise you are created a new long running detached session on each new login on the server and causing messages to be routed to all pending sessions. When those expires they are routed again etc.

In XMPP, the resource is the identifier of the device basically. You need to generate the JID for login with a string of form "user@domain/resource"

这篇关于XMPP推送通知邮件中造成问题(延迟+重复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆