Titanium JIRA Archive
Appcelerator Community (AC)

[AC-591] DeviceToken null on Android

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionCannot Reproduce
Resolution Date2016-02-07T05:52:31.000+0000
Affected Version/sn/a
Fix Version/sn/a
Componentsn/a
Labelsn/a
ReporterSwapnil Singh
AssigneeShak Hossain
Created2016-02-01T10:14:42.000+0000
Updated2016-02-07T05:52:31.000+0000

Description

Hello, Using Ti.cloudpush module i m able to retrieve devicetoken on android, but sometimes the devicetoken returned is null, when i re-install the app, it successfully returns a devicetoken

Comments

  1. Sharif AbuDarda 2016-02-02

    In which device you are trying? Try the below code
       var 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.showTrayNotification = 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();
       });
       
       var submit1 = Ti.UI.createButton({
           title : 'Subscribe Device',
           color : '#000',
           height : 60,
           width : 200,
           top : 200,
       });
       
       win.add(submit1);
       
       submit1.addEventListener('click', function(e) {
           defaultSubscribe();
       });
       
       var submit2 = Ti.UI.createButton({
           title : 'Unsubscribe Device',
           color : '#000',
           height : 60,
           width : 200,
           top : 300,
       });
       
       win.add(submit2);
       
       submit2.addEventListener('click', function(e) {
           defaultUnSubscribe();
       });
       
       CloudPush.retrieveDeviceToken({
           success : deviceTokenSuccess,
           error : deviceTokenError
       });
       
       function deviceTokenSuccess(e) {
           deviceToken = e.deviceToken;
       }
       
       function deviceTokenError(e) {
           alert('Failed to register for push notifications! ' + e.error);
       }
       
       CloudPush.addEventListener('callback', function(evt) {
           alert("Notification received: " + evt.payload);
       });
       
       CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
           Ti.API.info('Tray Click Launched App (app was not running)');
       });
       
       CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
           Ti.API.info('Tray Click Focused App (app was already running)');
       });
       
       function loginDefault(e) {
           Cloud.Users.login({
               login : 'User Email',
               password : 'Pass'
           }, function(e) {
               if (e.success) {
                   alert("Successfully Loged In");
               } else {
                   alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
       
       function defaultSubscribe() {
           Cloud.PushNotifications.subscribe({
               channel : 'alert',
               device_token : deviceToken,
               type : 'android'
           }, function(e) {
               if (e.success) {
                   alert('Device Subscribed!');
               } else {
                   alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
       
       function defaultUnSubscribe() {
           Cloud.PushNotifications.unsubscribe({
               channel : 'alert',
               device_token : deviceToken,
               type : 'android'
           }, function(e) {
               if (e.success) {
                   alert('Device Unsubscribed!');
               } else {
                   alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
       
       win.open(); 
       
    The code is self-explanatory. Create a user in your arrowDB in the platform and provide the email and pass in loginDefault(). First login and then subscribe the device and send push notification. Thanks.
  2. Swapnil Singh 2016-02-02

    The code applied is same as mentioned in Appcelerator Documentation: // Require the module var CloudPush = require('ti.cloudpush'); var deviceToken = null; // Initialize the module CloudPush.retrieveDeviceToken({ success : deviceTokenSuccess, error : deviceTokenError }); // Enable push notifications for this device // Save the device token for subsequent API calls function deviceTokenSuccess(e) { deviceToken = e.deviceToken; //alert(deviceToken); } function deviceTokenError(e) { //alert('Failed to register for push notifications! ' + e.error); } // Process incoming push notifications CloudPush.addEventListener('callback', function(evt) { Ti.API.info("NOTIFICATION :" + JSON.stringify(evt)); }); And we r using AWS SNS for sending push notifications to the device
  3. Sharif AbuDarda 2016-02-03

    What device you are generating the issue. Give us the model number. Also, make sure you use ti.cloudpush 3.4.1 module version.
  4. Swapnil Singh 2016-02-03

    I see to the ti.cloudpush version, whereas i have been facing this issue on Motorola G3 series phones for now

JSON Source