Android上的azure推送通知

时间:2015-01-15 17:15:06

标签: azure-mobile-services

我正在使用天蓝色移动服务(而不是集线器)在Android和iOS上发送推送通知

对于iOS推送通知工作正常,我得到deviceToken并向该devide发送推送

在Android上我获得了注册ID并发送推送,但我在设备上没有得到任何东西:

我的权限:

<!-- 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>

Handler代码:

@覆盖     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());
}

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);
                        }
                    });
                }
            }

所以我使用andorid上的registrationid向设备发送推送,而AZURE控制台说它确实发送了推送:

  

信息已发送推送:{multicast_id:5869048580698496000,成功:   1,失败:0,canonical_ids:0,结果:[{message_id:   '0:1421340390672415%29c0b12cf9fd7ecd'}],invalidIds:[],
  updatedIds:{}}¡Pronto! Tienes que ayudar a ...,esta a punto de   CAER。 ¡Llámale!

与移动服务相关的代码:

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());
        }

在处理程序上:

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();



    }
  

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

它适用于Android 4.2+

我在Android 4.0上测试,我不知道为什么它在该版本上无法工作,因为手机确实从其他应用程序获得通知,但在4.2和4.4测试后,通知工作正常。

希望能帮助其他人解决同样的问题

答案 1 :(得分:0)

如果您在服务器中发送:

push.gcm.send(pushId, message);

您必须在APP处理程序中注册pushId目标:

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

在APP处理程序中,pushId必须是String []。