How to add “ParsePushBroadcastReceiver.getActivity

2020-07-18 08:43发布

In which file of an application do I need to add getActivity method of ParsePushBroadcastReceiver?

Thanks.

3条回答
欢心
2楼-- · 2020-07-18 09:22

Write your on Receiver extends ParsePushBroadcastReceiver and your activity with the help of below code.

public class ParsePushReciever extends ParsePushBroadcastReceiver {

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

Note: Your activity must be a launcher activity.

查看更多
Rolldiameter
3楼-- · 2020-07-18 09:23

Usually you should add it to the class that extends the ParsePushBroadcastReceiver e.g.

public class YourBroadcastReceiver extends ParsePushBroadcastReceiver {

  ....
  protected Activity getActivity(Context context, Intent intent) {
    return yourActivity; // the activity that shows up 
  }
  ....

}
查看更多
Summer. ? 凉城
4楼-- · 2020-07-18 09:36

If you followed the documentation for an existing project like me, this is deprecated:

PushService.setDefaultPushCallback(this, YourActivity.class);

This works to me:

Implement your receiver and extends ParsePushBroadcastReceiver class.

public class Receiver extends ParsePushBroadcastReceiver {

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

        //To track "App Opens"
        ParseAnalytics.trackAppOpenedInBackground(intent);

        //Here is data you sent
        Log.i(tag, intent.getExtras().getString( "com.parse.Data" ));

        Intent i = new Intent(context, HomeActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Then, in your AndroidManifest.xml, (Instead of using ParsePushBroadcastReceiver)

<receiver
    android:name="your.package.name.Receiver"
    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>
查看更多
登录 后发表回答