Android - Parse push notification crashes on open

2019-08-30 17:57发布

问题:

I have set up parse push notifications and I had my app crash when I tried to open it, now I found a work around my making a new java class and overriding onPushOpen like this:

public class Receiver extends ParsePushBroadcastReceiver {

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

But in order to still receive push notifications I still need this depreciated method in my MyApplication.java class PushService.setDefaultPushCallback(this, MainActivity.class);

How could I get rid of this depreciated method I have looked at this question where I got some help but it did not answer this part about the depreciated method. Exception when opening Parse push notification.

I was thinking that maybe this method could be over ridden but Im not sure if it acutely handles recvieving the push or more handles the push after it has been received?

@Override
    public void onPushReceive(final Context c, Intent i) {
        // Handle the received push
    }

Thanks for the help in advance.

回答1:

You are subclassing ParsePushBroadcastReceiver.

Then in manifest

    <receiver
        android:name=".Receiver " // your broadcastreceiver
        android:exported="false" >
        <intent-filter>
            // youtr actions
        </intent-filter>
    </receiver>

In BroadCastReceiver

public class Receiver extends ParseBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    super.onReceive(context, intent);

        extras = intent.getExtras();
        if(intent.hasExtra("com.parse.Data")) 
        {
            try
            {
             json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
             int notificationtype = json.getInt("notificationtype"); // this is send on the sender side
             switch(notificationtype)
             {
              case 1:

                    // show your custom notification. Refer android notification guide 

                break;
                case 2:
                //rest of the code

Note : If either "alert" or "title" are specified in the push, then a Notification is constructed using getNotification. So no alert and title on the sender side.

Read Managing Push Lifecycle @

https://www.parse.com/docs/push_guide#receiving/Android

Reference

https://www.parse.com/questions/how-suppress-push-notification-from-being-displayed