1

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

}
1
  • no one with any idea? thanks Commented Jul 17, 2015 at 9:23

1 Answer 1

1

Aren't you missing an @Override note at all? The signature of the method should be:

@Override 
protected void onPushReceive(Context mContext, Intent intent) 

When I have had problems with notifications, I have added a line: android:name=".MyApplication" to the application tag in the Android manifest and it helped me.

Also I'm not sure that yours numerating system is good. When you relaunch app, counting starts from 1 again. I suppose that you are using notification id while showing it for users and now it didn't work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.