2
votes

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

2
Are you using a Mobile Service that is specifically set to NOT use a Notification Hub? (any newish Mobile Service automatically uses Notification Hubs behind the scenes). Can you add the code you're using to register you client with your Mobile Service to do push notifications?Chris
Hi Cris, yes I am using old code, no "enhanced push notificions" no conection to a notification hub. ill edit to add the codeAdrian Valdez

2 Answers

0
votes

Well it turnout that it works on Android 4.2+

I was testing on Android 4.0, I do not know why it doesnt work on that version since the phone does get notifications from other apps, but after testing in 4.2 and 4.4 the notifications work fine.

Hope that helps others with the same problems

0
votes

If in the server you send:

push.gcm.send(pushId, message);

You have to register the pushId target in your APP handler:

CT_CheckAge.mClient.getPush().register(gcmRegistrationId, pushId);

In APP handler, pushId have to be String[].