Openfire : Sending Push Notifications while recipient is offline

Problem : Sometimes push notifications are not sent while the recipient is offline.


Solution

* To store messages, while the recipient is offline :

Trigger

--
-- Triggers `ofOffline`
--
DELIMITER //
CREATE TRIGGER `PushNotification` AFTER INSERT ON `ofOffline`
 FOR EACH ROW BEGIN

DECLARE strMessageText VARCHAR(500) DEFAULT '';
 DECLARE strSenderId VARCHAR(500) DEFAULT ''; 
 DECLARE strReceiverId VARCHAR(500) DEFAULT ''; 
 DECLARE intMessageId INT DEFAULT 1;

SET strMessageText = ExtractValue(NEW.stanza, 'message/body[1]');
 SET strSenderId = ExtractValue(NEW.stanza, 'message/@from[1]');
 SET strReceiverId = ExtractValue(NEW.stanza, 'message/@to[1]');
 SET intMessageId = NEW.messageID; 
 INSERT INTO push_notification (message_id,from_user_id,to_user_id,message) VALUES (intMessageId,strSenderId,strReceiverId,strMessageText);


END
//
DELIMITER ;

Table :

CREATE TABLE IF NOT EXISTS `push_notification` (
`id` int(11) NOT NULL,
  `message_id` int(11) NOT NULL,
  `from_user_id` text NOT NULL,
  `to_user_id` text NOT NULL,
  `message` text NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ;

* API : callback-on-offline
// API for sending the push notification while the recipient is offline.

* Cron Jobs :


* * * * * curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 5; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 10; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 15; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 20; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 25; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 30; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 35; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 40; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 45; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 50; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1
* * * * * sleep 55; curl –silent http://inheritxdev.net/<path-to-api>/api/callback-on-offline >/dev/null 2>&1


// ^http://inheritxdev.net/<path-to-api>/api/callback-on-offline should be updated according to the project where we are deploying this solution.

Got a better solution? Please do let me know in comments..

Dhaval Baraiya

Believe in a Happier future >> | Stay Positive! (happy)

You may also like

Leave a Reply