I have implemented Parse push notifications but there is an issue on the Android side for me. Whenever I send a notification from Parse, all Android devices receive two notifications, one with the text that I have sent and the other with empty text.
Please suggest what might be wrong here
Note : I have also added native Android notifications (GCM notifications) to my project. Is it possible that they are causing this issue?
Thanks.
The AndroidManifest is setup as follows:
<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>
And this is my Broadcast Receiver :
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, 0, 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());
}
}
Parse uses GCM, so if you are sending a notification to an application that uses Parse, and in addition you handle the GCM message with native Android code that displays a notification, the notification may be handled twice (one time by your code and one time by Parse), which will cause it to be displayed twice.
As to why one of the notifications has empty text - the code that displays it probably expects the text to be in an extra parameter that wasn't sent by the server, and therefore there was no text to display.
UPDATE:
Now that I see you code, I can add the following :
You have two broadcast receivers that handle incoming GCM messages - com.parse.GcmBroadcastReceiver
and com.company.activity.GCBroadcastReceiver
. Both of them attempt to process the notifications sent to your device from Parse (from your comment below, I understand that the problem doesn't occur when you send your "local" notifications).
I'm assuming that the Parse library registers on its own to GCM. If you are using a different sender ID (API project ID) for the parse registration and for your native GCM registration, you can fix your problem by checking intent.getExtras ().get("from")
in your onReceive
method. If it doesn't contain the sender ID of your local notifications, don't call sendNotification(message, notificationID);
, and the second notification with the empty text won't be shown.
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);
}
Now, if you are using the same sender ID for both types of notifications, you can either start using different sender IDs, or you can use a different condition - check if intent.getExtras ().get("message")
has a value. Your local notifications require this parameter, and I'm assuming the Parse notifications don't (which explains the notifications without text you are seeing).
Using two GCM broadcast receivers may cause other problems too, so I suggest you look at this question for further reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With