Hello guys,
my callback method is called, if click the user to push notification button.
i want in my app a list of all push notifications because i need a callback methode if a push notification comm in.
this is my current code for push notification:
~~~
var CloudPush;
var notificationModel = require("lib/notificationModel");
if (OS_ANDROID) {
CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
}
var deviceToken;
var Cloud = require('ti.cloud');
Cloud.debug = true;
function loginDefault(e) {
//Create a Default User in Cloud Console, and login
Cloud.Users.login({
login : 'login',
password : 'password'
}, function(e) {
if (e.success) {
defaultSubscribe();
} else {
//alert("Login Fehlgeschlagen");
}
});
}
function defaultSubscribe() {
var channelName;
if (OS_IOS) {
channelName = "alertios";
} else if (OS_ANDROID) {
channelName = "alertandroid";
}
Cloud.PushNotifications.subscribe({
channel : channelName,
device_token : deviceToken,
type : (OS_IOS) ? "ios" : "android"
}, function(e) {
if (e.success) {
} else {
//alert(e);
}
});
}
function openWebView(pushurl) {
var pushNewsWebview = Alloy.createController("webView", {
url : pushurl
}).getView();
exports.tabGroup.activeTab.open(pushNewsWebview, {
animated : true
});
}
function pushNotificationCallback(evt) {
Ti.API.info('pushNotificationCallback ' + evt);
if (OS_IOS) {
Ti.API.info(evt);
Alloy.Globals.pushAndSaveCurrentNotificationsCount(evt.data.badge);
Alloy.Globals.main.notificationTab.setBadge(Alloy.Globals.currentNotificationsCount);
var notificationObjects = notificationModel.createNotificationObject(evt.data.alert, evt.data.badge);
Alloy.Global.addNotificationObjectToNotifications(notificationObjects);
var dialog = Ti.UI.createAlertDialog({
cancel : 0
});
dialog.title = "Alert";
dialog.message = evt.data.alert;
if (evt.data.pushurl) {
dialog.buttonNames = ['OK', "Öffnen"];
} else {
dialog.buttonNames = ["OK"];
}
dialog.addEventListener('click', function(e) {
Ti.UI.iPhone.setAppBadge(Ti.UI.iPhone.getAppBadge() - evt.data.badge);
switch(e.index) {
case 1:
openWebView(evt.data.pushurl);
break;
}
});
dialog.show();
} else if (OS_ANDROID) {
var payload = JSON.parse(evt.payload);
var dialog = Ti.UI.createAlertDialog({
cancel : 0
});
var notificationObjects = notificationModel.createNotificationObject(payload.android.alert, 1);
Alloy.Global.addNotificationObjectToNotifications(notificationObjects);
dialog.title = payload.android.title;
dialog.message = payload.android.alert;
if (payload.pushurl) {
dialog.buttonNames = ['OK', "Öffnen"];
} else {
dialog.buttonNames = ["OK"];
}
dialog.addEventListener('click', function(e) {
switch(e.index) {
case 1:
openWebView(payload.pushurl);
break;
}
});
dialog.show();
}
}
if (OS_ANDROID) {
CloudPush.addEventListener('callback', function(e) {
pushNotificationCallback(e);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
});
}
exports.tabGroup = null;
exports.setup = function() {
if (Ti.Network.online) {
if (OS_ANDROID) {
if (CloudPush.isGooglePlayServicesAvailable != CloudPush.SUCCESS) {
switch(CloudPush.isGooglePlayServicesAvailable) {
case CloudPush.SERVICE_DISABLED:
alert(L("cloudPushDisaple"));
break;
case CloudPush.SERVICE_INVALID :
alert(L("cloudPushInavlid"));
break;
case CloudPush.SERVICE_MISSING:
alert(L("cloudPushMissing"));
break;
case CloudPush.SERVICE_VERSION_UPDATE_REQUIRED:
alert(L("cloudPushUpdate"));
break;
default:
alert(L("cloudPushInavlid"));
break;
}
return null;
} else {
CloudPush.retrieveDeviceToken({
success : function(e) {
deviceToken = e.deviceToken;
loginDefault();
},
error : function(e) {
//alert(e.error);
}
});
}
} else if (OS_IOS) {
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
function registerForPush() {
Ti.Network.registerForPushNotifications({
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
// Remove event listener once registered for push notifications
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
};
// Wait for user settings to be registered before registering for push notifications
Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
// Register notification types to use
Ti.App.iOS.registerUserNotificationSettings({
types : [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
});
} else {
// For iOS 7 and earlier
Ti.Network.registerForPushNotifications({
// Specifies which notifications to receive
types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
success : deviceTokenSuccess,
error : deviceTokenError,
callback : receivePush
});
}
// Process incoming push notifications
function receivePush(e) {
pushNotificationCallback(e);
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
deviceToken = e.deviceToken;
loginDefault();
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
}
}
};
~~~
Thank's for your help.
↧