Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Notifications

Ianko Leite edited this page Dec 13, 2016 · 5 revisions

Notifications

Notifications manager

With this module you can handle the notifications system both on iOS and Android.

The Android module (written by us) is Ti.Goosh, and handles all Ti.Network features with the same syntax.

Receiving notifications

First of all, you have to set a driver in your config.json under T.

In alloy.js file just write:

var Notifications = require('T/notifications');

// Your implementation when a notification is received
Notifications.onReceived = function(e) {
	if (e.data.alert) {
		alert(e.data.alert);
	}
};

Or, to handle in your controller, you can always access to global event (be sure to remove it on controller close).

Notifications.on('received', function(e) {
	// ...
});

Activate notifications

When you are ready to subscribe the user (this method will prompt an alert on iOS), call:

Notifications.subscribe(/* channel */);

This will register the device to the APN/GCM servers, will cache the token for future activations in the same session and will send that token (plus other informations about the app/platform) to the specified driver.

Interactive notifications

Starting on iOS 8, you can define interactive notifications.

The original Titanium/iOS syntax is horrible, so we have redefined that:

Notifications.addInteractiveNotification("ARTICLE", [
	{
		id: "ARTICLE_READ",
		title: "Read",
		openApplication: true,
		callback: function() {
			Alloy.createController("article", { id: e.data.id_article })
		}
	},
	{
		id: "ARTICLE_OFFLINE",
		title: "Mark to read offline",
		callback: function() {
			Alloy.createModel("article", { id: e.data.id_article }).download();
		}
		}
	}
]);

Now, from your server, just send a payload containing almost:

{
	"category": "ARTICLE",
	"id_article": 2
	// ...
}