I am triying to send push notifications on Android and iOS using azure mobile services (not hubs)
For iOS push notifications are working fine, I get the deviceToken and send a push to that devide
On Android I get the registration ID and send a push, But I dont get anything on the device:
My permissions:
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--
Creates a custom permission so only this app can receive its messages.
NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
where PACKAGE is the application's package name.
-->
<permission android:name="com.sebiz.x_blockerEx.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sebiz.x_blockerEx.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- This app has permission to check your contacts -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- Needed for MobileAppTracking SDK -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ....
I got the receiver
<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.sebiz.blockerEx" />
</intent-filter>
</receiver>
Also the Handler code:
@Override public void onReceive(Context context, Bundle bundle) { ctx = context; String nhMessage = bundle.getString("message"); sendNotification(nhMessage); }
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, ACT_Home.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ALERTA")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
The push script for android...
if (action == "Unblock") {
var payload = "¡Pronto! Tienes que ayudar a " + userName + ", esta a punto de caer. ¡Llámale!";
var pushId = results[0].deviceToken;
console.log("Sending the push notification for unblock to device..." + results[0].deviceToken);
if (results[0].device == 'android') {
push.gcm.send(pushId, "¡Pronto! Tienes que ayudar a " + userName + ", esta a punto de caer. ¡Llámale!", {
success: function(pushResponse) {
console.log("Sent push:", pushResponse, payload);
},
error: function (pushResponse) {
console.log("Error Sending push:", pushResponse);
}
});
}
else if( results[0].device == 'iPhone'){
var dtoken = results[0].deviceToken;
push.apns.send(dtoken, {
alert: "¡Pronto! Tienes que ayudar a " + userName +", esta a punto de caer. ¡Llámale!",
payload: {
"inAppMessage" : "¡Pronto! Tienes que ayudar a " + userName +", esta a punto de caer. ¡Llámale!"
},
error: function(error) {
console.log('Error sending push notification: ', error);
}
});
}
}
So i use the registrationid on andorid to send push to device and the AZURE console says it did send the push:
INFORMATION Sent push: { multicast_id: 5869048580698496000, success: 1, failure: 0, canonical_ids: 0, results: [ { message_id: '0:1421340390672415%29c0b12cf9fd7ecd' } ], invalidIds: [],
updatedIds: {} } ¡Pronto! Tienes que ayudar a ..., esta a punto de caer. ¡Llámale!
Code to conect to mobile service:
try {
mClient = new MobileServiceClient(
"https://bloqueatuex.azure-mobile.net/",
"**********", //I put *** so i do not make my key public
this
).withFilter(new ProgressFilter());
//mUsersTable = mClient.getTable("users", usersAzure.class);
Log.i(TAG,"MS_Azure_INITIALIZE" +"FRESH INITIALIZED");
//fetchUserDetailsandInsertToAzure(Session.openActiveSession(ACT_Home.this, true, callback));
//Log.e("AZURE","azure_InsertedUserData: TRUE");
NotificationsManager.handleNotifications(this, SENDER_ID, azureHandler.class);
} catch (MalformedURLException e)
{
Log.e(TAG,"MS_Azure_ " +e.getMessage());
}
On the handler:
public class azureHandler extends com.microsoft.windowsazure.notifications.NotificationsHandler {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onRegistered(Context context, final String gcmRegistrationId) {
super.onRegistered(context, gcmRegistrationId);
ctx = context;
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
try {
if (ACT_CheckAge.mClient != null) {
ACT_CheckAge.mClient.getPush().register(gcmRegistrationId, null);
}
return null;
} catch (Exception e) {
Log.e("AZURE", "Error en registro con gcm...error:"+ e);
}
return null;
}
}.execute();
}
Any Help will be greatly appreciated