Parse Push Notifications Android to appear once the app is opened - android

I have done a lot of reading on Parse push notifications. I have push notifications working perfectly if the app is running or sitting in the background (user gets out of the app by pushing the home button or back button). I know I cannot get notifications to appear if the app has been force quit but if I close the app/force quit the app, is there a way to get the notifications to pop up the next time the app opens?
I have implemented MainApplication and made sure my manifest is correct but if the app is completely closed, then I do a push notification from Parse and then go to open the app it is as if it has not received it and never will display. Is that normal behavior?
Thanks,
Adam
Main Application
public class MainApplication extends Application {
private static MainApplication instance = new MainApplication();
public MainApplication()
{
instance=this;
}
public static Context getContext()
{
return instance;
}
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
//initialise whatson from parse.com
Parse.initialize(this, "xxxxx", "xxxxx");
PushService.setDefaultPushCallback(this, MainActivity.class);
//to register device to get push notifications and register the install
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}

Related

How can I completely unsubscribe from getting push notifications?

In my application I have implemented Push Notification functionality.
I added a button for the user, which allows him to turn off the notification completely. However it doesn't work as I excpected. Here's the code snippet:
From MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().subscribeToTopic("test_topic");
((SwitchCompat)findViewById(R.id.notification_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(MainActivity.this, "Switch activated ", Toast.LENGTH_LONG).show();
FirebaseMessaging.getInstance().subscribeToTopic("test_topic");
}else {
Toast.makeText(MainActivity.this, "Switch deactivated ", Toast.LENGTH_LONG).show();
FirebaseMessaging.getInstance().unsubscribeFromTopic("test_topic");
new Thread(new Runnable() {
#Override
public void run() {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
});
}
FirebaseMessagingService class to handle the message:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = MyFirebaseMessagingService.class.getName();
#Override
public void onMessageReceived(RemoteMessage remoteMessage){
super.onMessageReceived(remoteMessage);
Log.e(TAG, "onMessageReceived() --> " + remoteMessage.getNotification().getTitle());
}
// #Override
// public void onNewToken(String s) {
// super.onNewToken(s);
// Log.e(TAG, "onNewToken() --> " + s);
// }
}
When I try to send a notification using topic I don't get any notification. If I send the notification to all devices with the app, I still get the notification. How can I completely prevent my app from getting notifications?
Unregistration and Unsubscription
There are three methods a client app can use to stop receiving messages and/or topic subscriptions from the app server:
InstanceID deleteToken() with audience set to the app server's sender ID and scope set to GCM. This stops messages (including topic messages) from a specific app server
InstanceID deleteID(). This stops messages from all previous registrations and topic subscriptions for all app servers
GCMPubSub unsubscribe(). This stops topic messages from the specific topic of the app server
For FCM, it should be the FirebaseInstanceId.
Try to set setAutoInitEnabled(false) before FirebaseInstanceId.getInstance().deleteInstanceId().
When enabled, FCM generates a registration token on app startup if there is no valid one and generates a new token when it is deleted (which prevents deleteInstanceId() from stopping the periodic sending of data). This setting is persisted across app restarts and overrides the setting specified in your manifest.
By default, FCM auto-initialization is enabled.
For more information: FirebaseMessaging

Is there a way to bring an application to foreground on push notification receive?

For example in WhatsApp. When I call someone a activity opens up even if the app is in background or killed. How can I implement the same? Also I don't want my application to start a new activity cause this application will be used in a controlled environment. My application will always be in the background. The users will receive notifications when the app is open (background or foreground) so when they receive this notification they app should automatically app (not start a new activity) but just push the current activity to the foreground.
To best understand this is what I am trying to implement:
User A opens the app and then minimizes it. So now he is eligible to receive notifications. The application is currently on Activity A. When the user receives the notification we push the data received into a list and show it on Activity A. All I want to do is when User A receives a push notification to just push the Activity A to foreground no creating Activity A again or anything of that sort but just bring it to foreground. Any help would be much appreciated. Thank you!
You can use intent flag.
Intent.FLAG_ACTIVITY_SINGLE_TOP
And your code will be like
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
Intent intent= new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}
Follow this answer. You can register a broadcast in your activity, or directly call the method from your onMessageReceived()
On message received start NotificationActivity that will check if your application already running then finish that activity will bring you last opened activity on the stack, And if app not running will start your MainActivity
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
startActivity(new Intent(this , NotificationActivity.class));
}
}
public class NotificationActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// If this activity is the root activity of the task, the app is not running
if (isTaskRoot())
{
// Start the app before finishing
Intent startAppIntent = new Intent(getApplicationContext(), MainActivity.class);
startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startAppIntent);
}
// Now finish, which will drop the user in to the activity that was at the top of the task stack
finish();
}
}
Check this answer, and this answer for more details.

Handling custom payloads in OneSignal notification

I have integrated OneSignal in to my android app to use for push notifications, all is working fine and the notifications come through. However what do i need to change in order to handle these notifications in a custom manner? The receiver needs to parse the JSON payload from the notification and send the user to the correct point in the app based on what is received.
You need to setup a NotificationOpenedHandler class to handle the opening of a notification. See the following example code to set it up.
import com.onesignal.OneSignal;
public class YourAppClass extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
#Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
try {
if (additionalData != null) {
if (additionalData.has("actionSelected"))
Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
If you need to change which Activity is started when your app is opened see the OneSignal Changing the open action of a notification instructions.

How to show Parse Push Notification in notification bar while application is running?

I am using Parse to do push notifications and the issue I am running into is that while my application is running (either in the foreground or background) the phone's operating system does not show the push notification in the notification bar. What changes to my implementation do I need to make to see the push display on the notification bar?
My extended Application class has the following in onCreate()
// initialize Parse SDK
Parse.initialize(this, Constants.APPLICATION_ID_DEBUG, Constants.CLIENT_KEY_DEBUG);
ParsePush.subscribeInBackground(Constants.CHANNEL, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Logger.i(TAG, "successfully subscribed to broadcast channel");
} else {
Logger.e(TAG, "failed to subscribe for push: " + e);
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
I have a sign in system for my app, so I am using the ID of the logged in user as the Channel to subscribe users to. So in the first Activity of my app I call the following code snippet in onCreate().
private void registerNotifications() {
List<String> arryChannel = new ArrayList<String>();
arryChannel.add(session.id);
ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
parseInstallation.put("channels", arryChannel);
parseInstallation.saveEventually();
}
I also have a custom receiver that is working. Each time a push is sent out, it is being received by the onPushReceive method, however, I want the push to display in the notification bar.
public class ParsePushReceiver extends ParsePushBroadcastReceiver {
private static final String TAG = ParsePushReceiver.class.getSimpleName();
#Override
public void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "onPushOpen");
}
#Override
protected void onPushReceive(Context context, Intent intent) {
Log.i(TAG, "onPushReceive");
}
}
Thanks in advance!
Just remove the onPushReceive method and the default behaviour will remain (show the notification in the status bar.
You are getting this behaviour because if the application is running the Parse Push notification will call the method onPushReceive that does nothing.
I have figured this out. Although the answer provided by Sandra will make a push notification appear on the notification bar, it is not connected to Parse.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
This causes issues, because if you click on that notification the receiver you create extending ParsePushBroadcastReceiver will not register onPushOpen. My implementation for everything was correct, I only needed to add
super.onPushReceive(context, intent);
That will make the notification appear on the notification bar and also register clicks.
So make sure to make your receiver looks like this (at minimum)
public class ParsePushReceiver extends ParsePushBroadcastReceiver {
private static final String TAG = ParsePushReceiver.class.getSimpleName();
#Override
public void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "onPushOpen");
}
#Override
protected void onPushReceive(Context context, Intent intent) {
Log.i(TAG, "onPushReceive");
**super.onPushReceive(context, intent);**
}
}

Parse - Notification not being received

I am trying to send a Parse Push Notification from one Android application to all others.
The following is the set-up code in my Application object:
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(Game.class);
Parse.initialize(this, "code", "code");
ParsePush.subscribeInBackground(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
The following is the Push Notification code:
ParsePush push = new ParsePush();
String message = "Hello";
push.setChannel(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
push.setMessage(message);
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CreateGameActivity.this, "Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(CreateGameActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
}
});
break;
}
Even though the Success Toast is called, I still don't see the Notification appearing on any of the two Android devices I have installed the app on.
I have tested the Push Notifications via www.parse.com's Dashboard and that does work. Why won't it work in my app though?
To send notifications from a device, you have to do one extra step. Go into the settings of your app on parse and enable Client Push. That should resolve your issue.

Categories

Resources