[AC-1644] Push Notification on Android devices are not arriving and cause a Force Close
| GitHub Issue | n/a |
| Type | Bug |
| Priority | n/a |
| Status | Resolved |
| Resolution | Cannot Reproduce |
| Resolution Date | 2015-08-28T03:21:44.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | Titanium SDK & CLI |
| Labels | TCSupportTriage |
| Reporter | Fouad Kada |
| Assignee | Mostafizur Rahman |
| Created | 2014-10-15T10:33:48.000+0000 |
| Updated | 2016-03-08T07:38:07.000+0000 |
Description
Steps to Reproduce
- Run application on device
- enbale pussh notification by subscribing to channel
- send push notification from server backend using appcelerator's API
- mobile application will display force close
Actual Result
application force close and error is displayed in the studio's console
Expected Result
application does not force close and push notification arrives to device and is displayed in the notification tray
Attachments
error from the studio's console
Hi, Would you please provider us a test case to reproduce your problem. That will be really helpful. Thanks. Regards, Shuo
Hey, I can gladly share code for the titanium part and the wordpress plugin that is calling the notify in order to send push notifications....let me know what is most helpful to you. Regards, Fouad
Maybe the part of titanium that received push notification.
function TableMenu(parentWindow) { var Cloud = require('ti.cloud'); var osname = Ti.Platform.osname; var deviceTokenKey = 'deviceToken'; var pnEnabledKey = 'pnEnabled'; var firstBootKey = 'firstBoot'; var pnChannel = 'news'; var switchButton = Ti.UI.createSwitch(); function retrieveDeviceToken(cb) { // Enable push notifications for this device // Save the device token for subsequent API calls function deviceTokenSuccess(e) { Ti.API.info('successfully got device token'); Ti.App.Properties.setString(deviceTokenKey, e.deviceToken); cb(null); } function deviceTokenError(e) { Ti.API.error('Failed to register for push notifications! ' + e.error); Ti.App.Properties.setString(deviceTokenKey, ''); cb(e.error); } if (osname === 'android') { var CloudPush = require('ti.cloudpush'); CloudPush.debug = true; CloudPush.enabled = true; // Initialize the module CloudPush.retrieveDeviceToken({ success : deviceTokenSuccess, error : deviceTokenError }); // Process incoming push notifications CloudPush.addEventListener('callback', function(evt) { Ti.API.info("Notification received: " + evt.payload); var payload = JSON.parse(evt.payload); handlePushNotificationMessages(payload); }); } else { // Process incoming push notifications function receivePush(e) { Ti.API.info('Received push: ' + JSON.stringify(e)); var payload = e.data; handlePushNotificationMessages(payload); } // 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 }); } } } function subscribeToChannel() { // Subscribes the device to the 'news_alerts' channel // Specify the push type as either 'android' for Android or 'ios' for iOS Cloud.PushNotifications.subscribeToken({ device_token : Ti.App.Properties.getString(deviceTokenKey, ''), channel : pnChannel, type : Ti.Platform.name == 'android' ? 'android' : 'ios' }, function(e) { if (e.success) { Ti.API.info('Subscribed to channel'); Ti.App.Properties.setBool(pnEnabledKey, true); switchButton.value = true; } else { Ti.API.error('Error: ' + ((e.error && e.message) || JSON.stringify(e))); Ti.App.Properties.setBool(pnEnabledKey, false); switchButton.value = false; } }); } function unsubscribeToChannel() { // Unsubscribes the device from the 'test' channel Cloud.PushNotifications.unsubscribeToken({ device_token : Ti.App.Properties.getString(deviceTokenKey, ''), channel : pnChannel, }, function(e) { if (e.success) { Ti.API.info('Unsubscribed from channel'); Ti.App.Properties.setBool(pnEnabledKey, false); switchButton.value = false; } else { Ti.API.error('Error: ' + ((e.error && e.message) || JSON.stringify(e))); Ti.App.Properties.setBool(pnEnabledKey, true); switchButton.value = true; } }); } function handlePushNotificationMessages(payload) { var newsObj = { title : payload.postTitle, link : payload.postUrl, date : payload.postDate, time : payload.postTime, pubDate : '', description : '', image : '' }; newsObj.pushNotificationNews = true; var DetailView = require('ui/common/DetailView'); var detailView = new DetailView(newsObj, true); if (parentWindow) { var currentBadge = Ti.UI.iPhone.getAppBadge(); if (currentBadge > 0) { currentBadge = currentBadge - 1; Ti.UI.iPhone.setAppBadge(currentBadge); } if (parentWindow.isLeftWindowOpen()) { parentWindow.toggleLeftWindow(); } parentWindow.getCenterWindow().openWindow(detailView, { //this is mainly for ios devices animated : true }); } else { detailView.open(); } }; function handleSwitchButtonChanges(event) { if (event.value) { /** * i need to enable push notification */ retrieveDeviceToken(function(err) { if (!err) { subscribeToChannel(); } }); } else { /** * i need to disable push notification */ retrieveDeviceToken(function(err) { if (!err) { unsubscribeToChannel(); } }); } } function createSwitchButton(text) { switchButton.value = false; switchButton.itemId = 'swicthButton'; switchButton.right = '6dp'; var firstBoot = Ti.App.Properties.getBool(firstBootKey, true); if (firstBoot) { switchButton.value = true; retrieveDeviceToken(function(err) { if (!err) { subscribeToChannel(); Ti.App.Properties.setBool(firstBootKey, false); } else { switchButton.value = false; Ti.App.Properties.setBool(firstBootKey, true); } }); } else { var pnIsEnabled = Ti.App.Properties.getBool(pnEnabledKey, false); var tokenIsAvailable = Ti.App.Properties.getString(pnEnabledKey, ''); if (tokenIsAvailable && pnIsEnabled) { switchButton.value = true; } else { switchButton.value = false; } } switchButton.addEventListener('change', handleSwitchButtonChanges); if (osname === 'android') { switchButton.style = Titanium.UI.Android.SWITCH_STYLE_CHECKBOX; } var row = Ti.UI.createTableViewRow({ itemId : text, height : '40dp', width : Ti.UI.FILL, className : 'leftDrawer' }); var label = Ti.UI.createLabel({ text : L(text), color : 'black', font : { fontSize : '14dp', fontWeight : 'bold' }, itemId : text }); label.left = (osname === 'iphone') ? '15dp' : '3dp'; row.add(label); row.add(switchButton); return row; }; /** * will create a view to apply it as header view to the table */ var tableViewSectionHeaderView = Ti.UI.createView({ backgroundColor : 'black', height : '20dp' }); var dateHeaderView = Ti.UI.createLabel({ color : 'white', font : { fontSize : '13dp' }, left : '10dp', text : L('settings_title') }); tableViewSectionHeaderView.add(dateHeaderView); var settingsSection = Ti.UI.createTableViewSection({ headerView : tableViewSectionHeaderView }); settingsSection.add(createSwitchButton('push_notifications')); var table = Ti.UI.createTableView({ data : [settingsSection], backgroundGradient : { type : 'linear', startPoint : { x : '0%', y : '50%' }, endPoint : { x : '100%', y : '50%' }, colors : [{ color : '#e2dddb', offset : 0.0 }, { color : '#ebe6e5', offset : 0.25 }, { color : '#e2dddb', offset : 1.0 }], } }); return table; }; module.exports = TableMenu;Also would you please give me your app key for this problem, I can check the push notification status from our back end. For security season, you can sent the app key to my email directly. sliang@appcelerator.com
sent
Hi, Tried your code in my environment, can't reproduce your problem. So here is the suggest may help: 1. Try to sent a short payload notification from web console. 2. Try this simple code in your device, make sure it is not device problem.
3.Before run and test your app, clean your project first. 4.Would you please tell me which type of device did you use for the test. I noticed there are 3 android devices subscribed in Channel "aztag_news", did all of them have same problem? Hope this will help. Regard, Shuovar win = Ti.UI.createWindow({ backgroundColor : '#ccc', title : 'Android Cloud Push Notification' }); var CloudPush = require('ti.cloudpush'); CloudPush.debug = true; CloudPush.enabled = true; CloudPush.showTrayNotificationsWhenFocused = true; CloudPush.focusAppOnPush = false; var deviceToken=null; var Cloud = require('ti.cloud'); Cloud.debug = true; var submit = Ti.UI.createButton({ title : 'Register For Push Notification', color : '#000', height : 60, width : 200, top : 100, }); win.add(submit); submit.addEventListener('click', function(e) { loginDefault(); }); CloudPush.retrieveDeviceToken({ success : deviceTokenSuccess, error : deviceTokenError }); // Save the device token for subsequent API calls function deviceTokenSuccess(e) { deviceToken = e.deviceToken; } function deviceTokenError(e) { alert('Failed to register for push notifications! ' + e.error); } // Process incoming push notifications CloudPush.addEventListener('callback', function(evt) { alert("Notification received: " + evt.payload); }); function loginDefault(e) { // At first you need to create an user from the application dashboard // Then login that email and password Cloud.Users.login({ login : 'Email', password : 'pass' }, function(e) { if (e.success) { alert("login success"); defaultSubscribe(); } else { alert('Error: ' + ((e.error && e.message) || JSON.stringify(e))); } }); } function defaultSubscribe() { Cloud.PushNotifications.subscribe({ channel : 'alert', device_token : deviceToken, type : 'gcm' }, function(e) { if (e.success) { alert('Subscribed for Push Notification!'); } else { alert('Error:' + ((e.error && e.message) || JSON.stringify(e))); } }); } win.open();Hey, when i faced this problem i was sending really short messages like "test" or "test push notification". I am testing on a nexus 4 device. One thing i also am facing is that the notification never arrive if the app is not running and when i re open the application it displays a black screen and eventually force closes I also noticed the following in the console
Please try to create new project with my test code, see if it works well in your environment. The [ERROR] : GooglePlayServicesUtil: will not bother the push notification we used as long as you can get device token and subscribe channel.
is there any problems with the push notification service, yesterday i tried it and i received the push notifications while today i am not receiving anything
Hello, Now push notification works as expected and get in due time if the network connection is available. Also application does not crash. *Result:* Issue is not reproducible with latest SDK. *Steps to Test:* 1. Paste your code in app.js file. 2. Add cloud and cloudpush module from tiapp.xml file. 3. Set your API key and project ID to your dashboard project. 4. Run this code with the testing environment. 5. Subscribe and then send push from the dashboard. *Testing Environment:* Command-Line Interface, version 4.1.2, Ti SDK: 4.1.0.GA, Android Version: 4.2.2, 4.4.2, 5.1.0, ti.cloudpush Version: 3.3.8, ti.cloud Version: 3.2.9, Studio Version: 4.1.1 *Test Code*
Thanks// Require the module var CloudPush = require('ti.cloudpush'); var deviceToken = null; // Initialize the module CloudPush.retrieveDeviceToken({ success : deviceTokenSuccess, error : deviceTokenError }); // Process incoming push notifications CloudPush.addEventListener('callback', function(evt) { alert("Notification received: " + evt.payload); }); CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { alert("trayClickLaunchedApp"); }); CloudPush.addEventListener('trayClickFocusedApp', function(evt) { alert("trayClickFocusedApp"); }); // Save the device token for subsequent API calls function deviceTokenSuccess(e) { deviceToken = e.deviceToken; } function deviceTokenError(e) { alert('Failed to register for push notifications! ' + e.error); } var Cloud = require("ti.cloud"); function subscribeToChannel() { // Subscribes the device to the 'news_alerts' channel // Specify the push type as either 'android' for Android or 'ios' for iOS Cloud.PushNotifications.subscribeToken({ device_token : deviceToken, channel : 'news_alerts', type : Ti.Platform.name == 'android' ? 'android' : 'ios' }, function(e) { if (e.success) { alert('Subscribed'); } else { alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); } var win = Ti.UI.createWindow({ backgroundColor : "#fff" }); // Create a Button. var Subscribe = Ti.UI.createButton({ title : 'Subscribe', height : Ti.UI.SIZE, width : Ti.UI.SIZE, top : 20, }); // Listen for click events. Subscribe.addEventListener('click', function() { subscribeToChannel(); }); // Add to the parent view. win.add(Subscribe); win.open();