Let user enable/disable Push Notifications in Pars

2019-09-18 14:48发布

问题:

I'm developing an Android app that uses Push Notifications from Parse. On my settings menu I want to have an option to enable/disable push notifications but I can't find a way to do it using the parse library. All the ways I found online seem to be using methods that are now deprecated.

I'm not using channels, I just call Parse.initialize when starting the app.

Do you know how I can achieve this? To clarify I just need to know how to make the device ignore incoming notifications.

Regards

回答1:

Ok, I've worked out a solution. What I had to do was implement my own Receiver that replaces Parse's.

public class MyCustomReceiver extends ParsePushBroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       super.onReceive(context,intent);
   }
}

Then in AndroidManifest.xml replace this :

<receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        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>

with this (put your package name) :

<receiver
        android:name="your.package.name.MyCustomReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.example.UPDATE_STATUS" />
            <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>

Then rewrite your onReceive as you please, for instance, what I did was:

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    if (!sharedPrefs.contains("NOTIF") || sharedPrefs.getBoolean("NOTIF", false))
        super.onReceive(context,intent);
}

The variable NOTIF in SharedPreferences says if that user wants to receive notifications or not.