Let user enable/disable Push Notifications in Parse - android

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

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.

Related

Android studio: Element receiver duplicated

In my application, I would like to detect when my package is replaced and hence I have a receiver that is enabled in this way:
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="#bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="#bool/is_at_least_api_12" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
When importing my project from Eclipse to Android Studio, I got the following error:
Element receiver#com.x.y.ApplicationsReceiver at AndroidManifest.xml duplicated with element declared at AndroidManifest.xml.
Any idea how can I can solve this issue given hat I need to enable the receiver for different intent filters according to the Android API level?
Issue is occurring because adding same class two times in AndroidManifest for registering BroadcastReceiver for different Action.
Do it as by adding more then one Action in single BroadcastReceiver :
<receiver
android:name="com.x.y.ApplicationsReceiver"
android:enabled="#bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Now in onReceive method of ApplicationsReceiver class get Action from Intent and do what want to do according to API level:
#Override
public void onReceive(Context context,Intent intent) {
String action=intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.MY_PACKAGE_REPLACED")){
// do your code here
}else{
// do your code here
}
}

(Android) Subclass of ParsePushBroadcastReceiver doesn't get called on push notification. (parse.com)

I'm attempting to override the default behaviour of the receipt of push notifications from Parse.com's API on the android platform. As per various posts on SO, and in Parse's documentation - I've created the following class:
public class HHPBroadcastReceiver extends ParsePushBroadcastReceiver {
protected void onPushReceive(Context context,
Intent intent){
Log.d("DMM", "onPushReceive");
}
protected void onPushOpen(Context context, Intent intent) {
Log.d("DMM", "onPushOpen");
}
}
In my manifest, I'm also overriding the receiver as follows:
<!-- <receiver
android:name="com.parse.ParseBroadcastReceiver"
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> -->
<receiver
android:name="com.dreamr.hothalls.HHPBroadcastReceiver"
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>
Upon receipt of a push notification, my Receiver doesn't seem to get called - A notification appears in the notification area and clicking this carries out the default action. Nowhere in Logcat does my debug message appear.
I'm at my wit's end trying to work out what is most likely something incredibly simple I've overlooked.
Any advice or suggestions would be much appreciated,
Cheers,
Sean
You must register GCM broadcast receiver as well
<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="us.anyadir.admissiontable" />
</intent-filter>
</receiver>

Parse.com: Get Pushnotification Message on onPushOpen

In my android application I am using the push notifications supplied by Parse.com. I have successfully got this working, however my application has multiple push notifications and different activities should be loaded depending on which push notification has been pressed. I expected to be able to get the message for the push notification from the intent parameter of the onPushOpen, but it seems to be empty? . Does anyone know how I could get the message in the onPushOpen method?.
For some context, I have added my current custom PushReceiver code below.
public class PushReceiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, Splash.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Thanks.
Make sure you've upgraded to Parse-1.8.2.
Then you'll also need to do two more things.
Add information into your AndroidManifest.xml (within )
<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...ADD_PACKAGE_INFO_HERE..." />
</intent-filter>
</receiver>
<receiver android:name="com..ADD_PACKAGE_INFO_HERE....PushReceiver"
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>
Add this to AndroidManifest.xml outside the tags
Within the cloud code on Parse.com, set the uri of the broadcast to "com....ADD_PACKAGE_INFO_HERE...PushReceiver"
NB: #1 & #2 should be provided for you in the debug trace when you run your android app.
Hope this helps. I spent hours trying to get this to work and the key for me was updating to 1.8.2.
EDIT:
You should also look at overriding onReceive as well as onPushOpen...that actually gets called on a notification. Good luck.

How to detect that new app has been installed on the device?

How does CleanMaster app detect that a new app has been installed on the device? Whenever I install a new app, I get a popup asking if I want to move the app to SD card.
I am trying to code similar behaviour but cannot find a way to do it.
There is the ACTION_PACKAGE_ADDED Broadcast Intent, but the application being installed doesn't receive this.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Android provides String android.content.Intent.ACTION_PACKAGE_ADDED ="android.intent.action.PACKAGE_ADDED" Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.
You can write a BroadcastReceiver receiving the Intent.ACTION_PACKAGE_ADDED for that.
For this You need to write a receiver class like this
public class AppInstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
//Perform Your opeartion
}
}
And register it in manifest like.
<receiver android:name="com.example.AppInstallReceiver" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<data android:scheme="package" />
</intent-filter>
</receiver>

Android: receiving intent sent by system ACTION_PACKAGE_RESTARTED

I am new to android. I get completely stuck in using ACTION_PACKAGE_RESTARTED in my application
I have removed pacakge from my emulator, also added using adb install but get nothing. Start an app. close that one and again start that app. nothing seems work for me. There is no log in logcat.
Is there anything that i'm missing? Please help
public class RestartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
Log.i("D", "Inside receiver");
}
And here is the manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".ReceiverTest">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.ACTION_PACKAGE_RESTARTED" />
</intent-filter>
</receiver>
</application>
the value specified in the intent filter is incorrect..actual value is
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
and this broadcast can be received for other packages only. Restarted application/package doesn't receive this broadcast.
You should add a data specification to the intent-filter:
<data android:scheme="package" />

Categories

Resources