-2

Im trying to develop a cross platform application which will receive push notifications from Amazon SNS. The push notifications are working just fine for iOS, but for Android, im kind of at cross roads currently.

The push notifications are viewable when the Android App is focussed. However, no matter what variables related to ti.cloudpush i set(listed below).

The issue is - i am not able to get the push notifications to be shown in the notification tray.

CloudPush.showAppOnTrayClick    = true;
CloudPush.showTrayNotification  = true;
CloudPush.showTrayNotificationsWhenFocused= false;
CloudPush.singleCallback        =  true;

I guess this is related to Permissions that i might have to set iin the tiapp.xml for the Android Manifest section. I have included the list of permissions currently used below -

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk>14</uses-sdk>&gt;
    <manifest>
        <uses-sdk android:minSdkVersion="14"/>
        <permission android:name="com.test.push.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
        <uses-permission android:name="com.test.push.permission.C2D_MESSAGE"/>
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
        <uses-permission android:name="android.permission.VIBRATE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
        <uses-permission android:name="android.permission.USE_CREDENTIALS"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <application>
            <receiver
                android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
                <!-- Start receiver on boot -->
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    <action android:name="android.intent.action.USER_PRESENT"/>
                    <category android:name="android.intent.category.HOME"/>
                </intent-filter>
                <!-- Receive the actual message -->
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                    <category android:name="com.test.push"/>
                </intent-filter>
                <!-- Receive the registration id -->
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                    <category android:name="com.test.push.permission"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>
</android>

Could someone please let me know what i am doing wrong/ How to get this right ?

Any information/permissions related link would be highly appreciated.

FlyingV
  • 53
  • 1
  • 11

1 Answers1

0

Try this code :

app.js class :

/*
 Push notifications through device token.
 Steps : 
 1) Retrieve device token
 2) Subscribe for receiving notifications
 3) Notify
 4) Unsubscribe from receiving notifications
*/

 Titanium.UI.setBackgroundColor('#000');

var GcmWin = 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;
var Cloud = require('ti.cloud');
Cloud.debug = true;

var submit = Ti.UI.createButton({
  title : 'Retrieve Device token',
  color : '#000',
  height : 80,
  width : 200,
  top : 50
});

var subscribe = Ti.UI.createButton({
  title : 'Subscribe',
  color : '#000',
  height : 80,
  width : 200,
  top : 150
});
subscribe.addEventListener('click', subscribeToChannel);
GcmWin.add(subscribe);

var notify = Ti.UI.createButton({
  title : 'Notify',
  color : '#000',
  height : 80,
  width : 200,
  top : 250
});
notify.addEventListener('click', sendTestNotification);
GcmWin.add(notify);

var unsubscribe = Ti.UI.createButton({
  title : 'Unsubscribe',
  color : '#000',
  height : 80,
  width : 200,
  top : 350
});
unsubscribe.addEventListener('click', unsubscribeToChannel);
GcmWin.add(unsubscribe);

GcmWin.add(submit);

submit.addEventListener('click', function(e) {
CloudPush.retrieveDeviceToken({
    success : function deviceTokenSuccess(e) {
        alert('Device Token: ' + e.deviceToken);
        deviceToken = e.deviceToken;

    },
    error : function deviceTokenError(e) {
        alert('Failed to register for push! ' + e.error);
    }
 });
});

function subscribeToChannel() {
 // Subscribes the device to the 'test' channel
 // Specify the push type as either 'android' for Android or 'ios' for iOS
 Cloud.PushNotifications.subscribeToken({
    device_token : deviceToken,
    channel : 'test',
    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)));
    }
});
}

function sendTestNotification() {
// Sends an 'This is a test.' alert to specified device if its subscribed to the 'test'  channel.
 Cloud.PushNotifications.notifyTokens({
    to_tokens : deviceToken,
    channel : 'test',
    payload : 'This is a test.'
 }, function(e) {
    if (e.success) {
        alert('Push notification sent');
    } else {
        alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
    }
});
}

function unsubscribeToChannel() {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
    device_token : deviceToken,
    channel : 'test',
}, function(e) {
    if (e.success) {
        alert('Unsubscribed');
    } else {
        alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
    }
});
}

CloudPush.addEventListener('callback', function(evt) {
 //alert(evt);
 alert(evt.payload);
});

CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
 Ti.API.info('Tray Click Launched App (app was not running)');
 //alert('Tray Click Launched App (app was not running');
});

CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
 Ti.API.info('Tray Click Focused App (app was already running)');
 //alert('Tray Click Focused App (app was already running)');
});

In tiapp.xml just check this below lines.Add it, if not there :

 <modules>
    <module platform="commonjs">ti.cloud</module>
    <module platform="android">ti.cloudpush</module>
</modules>

This works for me. Hope this helps.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
  • Hi Siddharth. Thank you for your response. I had originally used the same logic as your answer states. However, the application would crash on receipt of the push notification. due to an error which was **-Unable to instantiate receiver java.lang.ClassNotFoundException** On research i found the following link which helped me get my push notification working when app is focussed. [link]http://stackoverflow.com/questions/9601373/unable-to-instantiate-receiver-java-lang-classnotfoundexception Does your code display notifications in the android notification tray? – FlyingV Jul 18 '14 at 08:39
  • Can you please post the error log details.This solution works for me. – Siddharth_Vyas Jul 18 '14 at 08:40
  • Yes this code works for android notification tray.Just give it a try.I used 3.2.1 GA version. – Siddharth_Vyas Jul 18 '14 at 08:44
  • Im currently using SDK 3.2.3 GA. Any thoughts on the same? On which SDK version is it working for you? – FlyingV Jul 18 '14 at 08:45
  • @FlyingV: What is the cloudpush version you are using? – Anand Jul 18 '14 at 08:47
  • @SiddharthVyas: the error i find in the log is as follows - 07-18 14:19:44.853: E/TiApplication(25319): (main) [9,46101] Sending event: exception on thread: main msg:java.lang.RuntimeException: Unable to start receiver ti.cloudpush.GCMReceiver: java.lang.NullPointerException; Titanium 3.2.3,2014/04/22 10:17,b958a70 07-18 14:19:44.853: E/TiApplication(25319): java.lang.RuntimeException: Unable to start receiver ti.cloudpush.GCMReceiver: java.lang.NullPointerException – FlyingV Jul 18 '14 at 08:52
  • @Anand: im using Ti.cloudpush version 3.2.1 – FlyingV Jul 18 '14 at 08:54
  • @FlyingV: Try changing the module to 2.3.3. – Anand Jul 18 '14 at 08:55
  • @FlyingV : Why are you using GCM?? You can use ACS that is built in cloud services for Titanium. – Siddharth_Vyas Jul 18 '14 at 08:59
  • @FlyingV: There are couple of issues posted in developer QA related to CloudPush module version 3.2.x. Myself also faced issues with CloudPush 3.2.x – Anand Jul 18 '14 at 09:00
  • @SiddharthVyas - Okay. I apologize if im not making much sense. Im a newbie if thats any reason. Let me try explaining. Im making use of ACS to retrieve device token and pass it to server which will register with the Amazon SNS service for me and take care of actually sending push notification. All i have to do is display received push. For some reason, even though im following the same event listeners that you have used, im unable to see the notifications on the tray. – FlyingV Jul 18 '14 at 09:03
  • @Anand - Thank you sir! I was originally developing the app using SDK version 3.0.1 GA, but due to same issue, i was asked to upgrade to SDK 3.2.3. Now, atleast it is not crashing like it was in 3.0.1. But i will try 2.3.3 Ti.Cloudpush as you suggest. Hope i have some luck. – FlyingV Jul 18 '14 at 09:05
  • @FlyingV : No need to apologize :). I just wanted to know what do you use for push notifications. :) – Siddharth_Vyas Jul 18 '14 at 09:06
  • @Anand - Im using Ti.Cloud version 3.2.3 – FlyingV Jul 18 '14 at 09:11