【发布时间】:2015-04-22 19:56:16
【问题描述】:
我正在尝试使用 azure 移动服务(不是集线器)在 Android 和 iOS 上发送推送通知
对于 iOS 推送通知工作正常,我获取 deviceToken 并向该设备发送推送
在 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>
还有处理程序代码:
@覆盖 公共无效 onReceive(上下文上下文,捆绑包){ ctx = 上下文; String nhMessage = bundle.getString("message"); 发送通知(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 上的注册 ID 向设备发送推送,而 AZURE 控制台说它确实发送了推送:
INFORMATION 发送推送:{ multicast_id: 5869048580698496000,成功: 1、失败:0、canonical_ids:0、结果:[{message_id: '0:1421340390672415%29c0b12cf9fd7ecd' } ], invalidIds: [],
updatedIds: {} } ¡ Tienes que ayudar a ..., esta a punto de 凯尔。 ¡拉马尔!
连接移动服务的代码:
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();
}
任何帮助将不胜感激
【问题讨论】:
-
您是否使用了专门设置为不使用通知中心的移动服务? (任何新的移动服务都会在后台自动使用通知中心)。您可以添加您用于向您的移动服务注册您的客户端以执行推送通知的代码吗?
-
嗨,克里斯,是的,我使用的是旧代码,没有“增强推送通知”,没有连接到通知中心。生病编辑以添加代码