Open Application After Clicking on Notification

Open application after clicking on Notification

See below code. I am using that and it is opening my HomeActivity.

    NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

Open app after clicking on Notification

Basically your Pending Intent should have your application's one of the activity components.

Intent appIntent = new Intent(context, YourActivityName.class);
PendingIntent.getActivity(context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Similarly you can add other components like Activity, Service and Receiver.

PendingIntent.getActivity ...
PendingIntent.getBroadcast ...
PendingIntent.getService ...

Hope this helps.

How can i open Activity when notification click

Yes, it is possible.

Change Your method, like that;

private void sendNotification(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(getNotificationIcon(), "Action Button", pIntent)
.setContentIntent(pIntent)
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

and add your mainactivity

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

this code working.

How to open application while clicking notification?

What do you mean by ask for password? just open an activity that asks for password.

As for open an activity use Nirali solution in: Open application after clicking on Notification

 NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

Open app from notification “App is running in the background”

Try this one.

package developer.eyosiyas.NileSat.Habesha.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import developer.eyosiyas.NileSat.Habesha.R;
import developer.eyosiyas.NileSat.Habesha.View.MainActivity;

public class ServiceExample extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

@Override
public void onCreate() {
super.onCreate();


Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("Player", "Sync Service", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(R.color.colorPrimary);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setDescription("Service Name");
notificationManager.createNotificationChannel(channel);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Player")
.setContentTitle("Music Player")
.setContentText("My Music")
.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setOngoing(true);

Notification notification = builder.build();
notificationManager.notify(1, notification);
startForeground(1, notification);
}
}

Android notification click to open app only works 1st time

I managed to fix it. The problem was that I was updating the notification with a intent that had the wrong class.

Open Activity on notification button click when app is closed

You can try to receive the click in a BroadcastReceiver and then open activity from there.

  1. Try this to add a action button o your notification:
timerNotificationBuilder.addAction(createNotificationActionButton("STOP");

Where the createNotificationActionButton method is this:

public NotificationCompat.Action createNotificationActionButton(String text){
Intent intent = new Intent(this, StopwatchNotificationActionReceiver.class);

@SuppressLint("InlinedApi") PendingIntent pendingIntent = PendingIntent.getBroadcast(this, new Random().nextInt(100), intent, PendingIntent.FLAG_IMMUTABLE);

return new NotificationCompat.Action(0, text, pendingIntent);
}

  1. Create a class named StopwatchNotificationActionReceiver and make it extent a BroadcastReceiver`. This is the code for that class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class StopwatchNotificationActionReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
PrefUtil.setIsRunningInBackground(context, false);
PrefUtil.setTimerSecondsPassed(context, 0);
PrefUtil.setWasTimerRunning(context, false);
context.stopService(MainActivity.serviceIntent);
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
}
}

Also you need to register that receiver in your manifest like this:

<receiver android:name="StopwatchNotificationActionReceiver"/>

  1. Where the MainActivity.serviceIntent is a public static variable which looks like this:
public static Intent serviceIntent;

And this intent is only used to start the service like this:

//In onCreate
serviceIntent = new Intent(this, TimerService.class);

//In onPause
PrefUtil.setTimerSecondsPassed(this,seconds);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
}







Or you can try the simple method:

if (action != null && action.equals(ACTION_STOP_SERVICE)) {
Context context = this;
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActvity(activityIntent);
killService();
}

Edit


Another solution is here. Again. You need to refer to my repo as I have made changes to the files in order to complete your task. In the service class, refer to this method. There, I start the activity if the action is reset(r). Or else, it opens the broadcast receiver. Then, in the activity, I receive that extra in the onResume() method. If the reset button is not clicked, it opens the Receiver class.

And as always, you can view the result of the app from here.

I hope that code will do your work.

Open the current screen after clicking the notification action button

Why are you using a BroadcastReceiver instead of an Activity for this purpose?

It would be better to launch an Activity in your app to show this "delete" dialog. This will bring your app to the foreground (in whatever state it was in) and launch the new Activity on top of that. You can show your dialog and when the dialog is complete you can finish() the Activity and drop the user back into your app in whatever state it was in.

If you only want the dialog to show up, you can either use a transparent Activity to host it, or you can use a dialog-themed Activity (an Activity that looks like a dialog) for this purpose.

How to start activity when user clicks a notification?

I figured out the problem. I forgot to include the package name in the activity declaration in the Manifest file.

Wrong:

activity android:name=".ViewTarget" android:label="@string/app_name" 

Correct:

activity android:name="com.kjdv.gpsVegas.ViewTarget" android:label="@string/app_name" 


Related Topics



Leave a reply



Submit