Campbell Brobbel
Campbell Brobbel

Reputation: 75

Running a function after the app opens from the background after notification click

I'm pretty new to Android development. I have been able to get a notification to pop up while the app is in the background. When I click on it, it successfully loads the application backup. However I want to load an Alert from the page but only when it is opened from a notification click.

Here is the code for generating the notification. Any help would be appreciated.

private void getNotificationForPasswordChange() {
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Hello";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        if (mNotificationManager != null)
            mNotificationManager.createNotificationChannel(mChannel);
    }

    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.mipmap.ic_launcher);
    Intent i=new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
            i, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Pronto Tracker")
            .setTicker("Pronto Tracker")
            .setContentText("Cannot connect to server. Location is not being updated.")
            .setSmallIcon(R.mipmap.ic_pronto_logo)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setOngoing(true).setContentIntent(mainIntent).
                    build();
    mNotificationManager.notify(Constants.PASSWORD_CHANGE_NOTIFICATION_ID, notification);
}

Upvotes: 0

Views: 56

Answers (2)

Raja chakraborty
Raja chakraborty

Reputation: 825

You can pass the alert message with notification PendingIntent. Add the message or value you want to show as alert in PendingIntent .putExtra() and also specify the activity in PendingIntent where you want to show the alert in form of dialog or anything.

 Intent intent = new Intent(Application.getAppContext(), MainActivity.class);
 intent.putExtra("is_notification", true);
 intent.putExtra("alert_message", "Hello World!");
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);

After that add the PendingIntent to your notification. Second thing you need to do is to get the data from the Intent when user taps on notification. In your MainActivity add the following code to get data from Intent:-

if (getIntent() != null) {
   String message = getIntent().getStringExtra("alert_message");
   boolean isNotification = getIntent().getBooleanExtra("is_notification", false);

    if(is_notification){
       // show alert
      }
    }

Upvotes: 1

SirotovAB
SirotovAB

Reputation: 1

You should use onCreate function on your MainActivity Add this code to parce your intent: Intent receivedIntent = getIntent();

Upvotes: 0

Related Questions