sulavvr
sulavvr

Reputation: 276

Android custom class extending ParsePushBroadcastReceiver notification

I am trying to work with Parse by extending the ParsePushBroadcastReceiver class to handle my push notifications. The problem I'm having is that the notification doesn't show up when I do a test push from the Parse dashboard. But I know I'm receiving the push because on my onReceive function, I log the message and it shows up. But the notification doesn't show up at all for me to work on the onPushOpen or to just add a check to see if the action is ACTION_PUSH_OPEN

This is in my manifest for my app.

<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.app.myapp.PushNotificationReceiver"
    android:exported="false">
    <intent-filter>
       <action android:name="com.parse.push.intent.RECEIVE" />
       <action android:name="com.parse.push.intent.DELETE" />
       <action android:name="com.parse.push.intent.OPEN" />
    </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.app.myapp" />
    </intent-filter>
</receiver> 

This is the custom push receiver class.

public class PushNotificationReceiver extends ParsePushBroadcastReceiver{

private static final String TAG = "PushNotificationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Received intent: " + intent.toString());
        String action = intent.getAction();
        Log.d("TEST", action.toString());
        if (action.equals(ParsePushBroadcastReceiver.ACTION_PUSH_RECEIVE)) {
            JSONObject extras;
            try {
                extras = new JSONObject(intent.getStringExtra(ParsePushBroadcastReceiver.KEY_PUSH_DATA));

                // I get this on my log like this:
                // Received push notification. Alert: A test push from Parse!

                Log.i(TAG, "Received push notification. Alert: " + extras.getString("alert"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

I hope it makes sense. Thanks!

EDIT:

I changed my manifest file to this:

<receiver android:name="com.parse.ParsePushBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND"
    android:exported="false">
    <intent-filter>
      <action android:name="com.parse.push.intent.RECEIVE" />
    </intent-filter>
</receiver>
<receiver android:name="com.app.myapp.PushNotificationReceiver"
    android:permission="com.google.android.c2dm.permission.SEND"
    android:exported="false">
    <intent-filter>
      <action android:name="com.parse.push.intent.DELETE" />
      <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>

Now the push notification shows up because of the default ParsePushBroadcastReceiver and now I can check if the action is DELETE or OPEN in my custom Receiver class' onReceive method. I don't know if this is a good solution, but at least the notification shows up now. Please tell me if there's another solution where I can use my own class for the RECEIVE action.

Upvotes: 1

Views: 2518

Answers (2)

sulavvr
sulavvr

Reputation: 276

Okay, so I changed my manifest back to

<receiver android:name="com.app.myapp.PushNotificationReceiver"
    android:permission="com.google.android.c2dm.permission.SEND"
    android:exported="false">
    <intent-filter>
      <action android:name="com.parse.push.intent.RECEIVE" />
      <action android:name="com.parse.push.intent.DELETE" />
      <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>

And I added a custom method to generate the notification in my PushNotificationReceiver class which I call in my onReceive method which takes the JSON payload as the second parameter (ParsePushBroadcastReceiver.KEY_PUSH_DATA):

private void generate_notification(Context context, JSONObject json){
    try {
        notification = json.getString("alert");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Intent i = new Intent(context, NotificationHandler.class);
    PendingIntent pintent = PendingIntent.getActivity(context, 0, i, 0);

    Notification notif = new NotificationCompat.Builder(context)
                        .setContentTitle("My App")
                        .setContentText(notification)
                        .setSmallIcon(R.drawable.ic_logo)
                        .setContentIntent(pintent)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setAutoCancel(true)
                        .build();

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notif);
}

I guess this answers my question on how.

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Override getNotification method from ParsePushBroadcastReceiver to show Notification :

@Override
protected Notification getNotification(Context context, Intent intent) {

 Intent notificationIntent = new Intent(context, TestActivity.class);    
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

 PendingIntent piIntent=PendingIntent.getActivity(context,0,
                                                        notificationIntent,0);

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
   .setContentTitle("My Notification")
   .setStyle(new NotificationCompat.BigTextStyle()
   .bigText(notificationMessage))
   .setContentText(extras.getString("alert")).setAutoCancel(true);        
    mBuilder.setContentIntent(piIntent);

    return mBuilder.build();
 }

Upvotes: 2

Related Questions