I am trying to send a push notification to a specific Android device. I set up the Backendless and Google Developer consoles as described here: http://backendless.com/documentation/messaging/android/messaging_push_notification_setup_androi.htm
and followed the documentation to register the device and create the push messages.
However, the push messages are not received.
I downloaded the Sample Chat code and it works perfectly. However, I don’t understand its logic very well so that I could implement it in my project.
This is an excerpt from my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.victor.test" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.victor.test.permission.C2D_MESSAGE" />
<permission
android:name="com.victor.sexytalk.sexytalk.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<application
android:name=".Test"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".PushReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.victor.test" />
</intent-filter>
</receiver>
<service android:name="com.backendless.AndroidService" />
</application>
</manifest>
This is my custom push receiver:
public class PushReceiver extends BackendlessBroadcastReceiver
{
@Override
public boolean onMessage( Context context, Intent intent )
{
CharSequence tickerText = intent.getStringExtra( PublishOptions.ANDROID_TICKER_TEXT_TAG );
CharSequence contentTitle = intent.getStringExtra( PublishOptions.ANDROID_CONTENT_TITLE_TAG );
CharSequence contentText = intent.getStringExtra( PublishOptions.ANDROID_CONTENT_TEXT_TAG );
String subtopic = intent.getStringExtra( "message" );
if( tickerText != null && tickerText.length() > 0 )
{
int appIcon = context.getApplicationInfo().icon;
if( appIcon == 0 )
appIcon = android.R.drawable.sym_def_app_icon;
//Intent notificationIntent = new Intent( context, AcceptChatActivity.class );
//notificationIntent.putExtra( "subtopic", subtopic );
//PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( context );
notificationBuilder.setSmallIcon( appIcon );
notificationBuilder.setTicker( tickerText );
notificationBuilder.setWhen( System.currentTimeMillis() );
notificationBuilder.setContentTitle( contentTitle );
notificationBuilder.setContentText( contentText );
notificationBuilder.setAutoCancel( true );
//notificationBuilder.setContentIntent( contentIntent );
Notification notification = notificationBuilder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
notificationManager.notify( 0, notification );
}
return false;
}
}
This is how I register the device:
Backendless.Messaging.registerDevice(GCMSenderID, channel, new AsyncCallback<Void>() {
@Override
public void handleResponse(Void aVoid) {
Log.d("Test", "device registered for messaging");
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Log.d("Test", "device not registered " + backendlessFault.getMessage());
}
});
This is how I send the push message:
PublishOptions publishOptions = new PublishOptions();
publishOptions.putHeader( PublishOptions.ANDROID_TICKER_TEXT_TAG, "Backendless" );
publishOptions.putHeader(PublishOptions.ANDROID_CONTENT_TITLE_TAG, getResources().getString(R.string.app_name));
publishOptions.putHeader(PublishOptions.ANDROID_CONTENT_TEXT_TAG, "Hi");
DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.setPushPolicy(PushPolicyEnum.ONLY);
deliveryOptions.addPushSinglecast( (String) mRecepient.getProperty("deviceId") );
Backendless.Messaging.publish( "this is a private message!", publishOptions, deliveryOptions, new AsyncCallback<MessageStatus>() {
@Override
public void handleResponse(MessageStatus messageStatus) {
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
String error = backendlessFault.getMessage();
}
} );
The problem is that the custom receiver is never called. I think the issue has something to do with the device registration as I cannot see the device in the Backendless console under the relevant channel name (There is a channel for each user).
Any help is greatly appreciated!