I'm using a custom notification receiver class for parse.com push notifications to change the sound, vibration... sometimes, it looks to me that the notification did not arrives. I can see the push notification on the parse website as send. Maybe someone can review my code and see some wrong code pieces.
Manifest:
<service android:name="com.parse.PushService" />
<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.mflapp.test" />
</intent-filter>
</receiver>
<receiver
android:name=".MyCustomReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.OPEN" />
<action android:name="com.parse.push.intent.DELETE" />
</intent-filter>
</receiver>
MyCustomReceiver
public class MyCustomReceiver extends ParsePushBroadcastReceiver {
private static int NOTIFICATION_ID = 1;
protected void onPushReceive(Context mContext, Intent intent) {
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
if (action.equalsIgnoreCase("com.parse.push.intent.RECEIVE")) {
NOTIFICATION_ID++;
String title = mContext.getResources().getString(R.string.app_name);
if (json.has("alert"))
{
String text = json.getString("alert");
generateNotification(mContext, title, json, text);
}
}
} catch (JSONException e) {
}
}
private void generateNotification(Context context, String title, JSONObject json, String text) {
Intent intent = new Intent(context, home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setTicker(text)
.setLights(Color.GREEN, 500, 500);
mBuilder.setContentIntent(contentIntent);
mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
}