我已经实施了Parse推送通知,但是在
Android方面有一个问题.每当我从Parse发送通知时,所有的Android设备都会收到两个通知,一个是我发送的文本,另一个是空文本.
请建议这里可能是错误的
注意:我还将本机Android通知(GCM通知)添加到我的项目中.他们是否可能导致这个问题?
谢谢.
AndroidManifest的设置如下:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.android.vending.BILLING" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <application android:name=".activityApplication" android:label="@string/app_name" android:icon="@drawable/icon"> <activity android:name=".activity" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="orientation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> <!-- gcm --> <receiver android:name="com.company.activity.GCBroadcastReceiver" android:exported="true" android:process=":remote" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.company.activity" /> </intent-filter> </receiver> <service android:name=".GcmIntentService" /> <service android:name="com.parse.PushService" /> <receiver android:name="com.parse.ParseBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> <receiver android:name="com.parse.GcmBroadcastReceiver" 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.company.activity" /> </intent-filter> </receiver> </application>
这是我的广播接收者:
public class GCBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "GCBroadcastReceiver"; Context ctx; @Override public void onReceive(Context context,Intent intent) { ctx = context; // Local notification. Bundle bundle = intent.getExtras(); String message = bundle.getString("message"); String notificationID = bundle.getString("notificationID"); Log.v(TAG,"Notification message : " + message + "With ID : " + notificationID); sendNotification(message,notificationID); } // Put the GCM message into a notification and post it. private void sendNotification(final String message,final String notificationID) { Intent notificationIntent = new Intent(ctx,Activity.class); PendingIntent intent = PendingIntent.getActivity(ctx,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx); mBuilder.setContentIntent(intent); mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setSmallIcon(R.drawable.icon); mBuilder.setContentTitle(ctx.getString(R.string.app_name)); mBuilder.setContentText(message); mBuilder.setLights(Color.RED,400,400); mBuilder.setAutoCancel(true); //mBuilder.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + ctx.getPackageName() + "/raw/" + sound)); mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); NotificationManager mNotificationManager = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0,mBuilder.build()); } }
解决方法
解析使用GCM,因此如果您向使用Parse的应用程序发送通知,并且另外处理显示通知的原生Android代码的GCM消息,通知可能会被处理两次(一次由您的代码和一次通过Parse),这将导致它被显示两次.
至于为什么其中一个通知有空文本 – 显示它的代码可能期望文本是在服务器不发送的额外的参数,因此没有文本显示.
更新:
您有两个处理传入GCM消息的广播接收器 – com.parse.GcmBroadcastReceiver和com.company.activity.GCBroadcastReceiver.他们都尝试处理从Parse发送到您的设备的通知(从下面的评论中,我了解当您发送“本地”通知时不会发生问题).
我假设Parse图书馆自己向GCM注册.如果您使用不同的发件人ID(API项目ID)进行解析注册和本地GCM注册,则可以通过在onReceive方法中检查intent.getExtras().get(“from”)来解决您的问题.如果不包含本地通知的发件人ID,请不要调用sendNotification(message,notificationID);并且不会显示带有空文本的第二个通知.
public void onReceive(Context context,Intent intent) { ctx = context; // Local notification. Bundle bundle = intent.getExtras(); String message = bundle.getString("message"); String notificationID = bundle.getString("notificationID"); Log.v(TAG,"Notification message : " + message + "With ID : " + notificationID); if (intent.getExtras ().get("from").equals (YOUR_SENDER_ID_FOR_LOCAL_NOTIFICATIONS)) sendNotification(message,notificationID); }
现在,如果您对两种类型的通知使用相同的发件人ID,您可以开始使用不同的发件人ID,也可以使用不同的条件 – 检查intent.getExtras().get(“message”)是否具有值.您的本地通知需要此参数,我假设Parse通知不会(这解释了您看不到文本的通知).
使用两个GCM广播接收机也可能会导致其他问题,所以我建议您查看this question进一步阅读.