When app is running and device is locked I'm able to start the activity. But when app is in background and device is locked not able to start the activity even though I'm getting the control in BroadcastReceiver class. This is my intent call.
context.startActivity(new Intent(context, ReceiveCallActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction(Intent.ACTION_ANSWER)
.putExtra("title", intent.getStringExtra("title"))
.putExtra("action", intent.getStringExtra("action")));
Manifest of Activity
<activity
android:name=".ReceiveCallActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:launchMode="singleTop"
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:turnScreenOn="true"
android:windowSoftInputMode="adjustPan|stateHidden" />
ReceiveCallActivity.class
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
setContentView(R.layout.receive_call_activity);
...
...
}
setShowWhenLocked(true) && setTurnScreenOn(true) helps to open app even if device is locked but app has to be in foreground for that.
PS: I'm getting the control in BroadcastReceiver in all scenarios.
TIA
I was checking different permissions given to Skype from Settings, and noticed 'Show on Lock Screen' is enabled while the same was disabled for my App. On enabling it, BroadcastReceiver is able to open Activity in all scenarios. I read it's a issue with Xiamoi devices(I'm using Note 5 Pro).
EDIT
For Android 10 need to add USE_FULL_SCREEN_INTENT permission in manifest.
Then when the screen is locked, PendingIntent set as FullScreenIntent on NotificationCompat.Builder will be called.
My Notification code:
private void showCallNotification(Map<String, String> dataMap) {
//CREATING pendingIntent
...
...
...
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 2, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 1, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
notificationLayout.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
notificationLayout.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
notificationLayout.setOnClickPendingIntent(R.id.tvAccept, pendingIntent);
notificationLayout.setOnClickPendingIntent(R.id.tvDecline, cancelPendingIntent);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
notificationLayoutExpanded.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
notificationLayoutExpanded.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btAccept, pendingIntent);
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btDecline, cancelPendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(dataMap.get("sender"))
.setContentText(getString(R.string.incoming_call))
.setAutoCancel(true)
.setTimeoutAfter(CALL_DISMISS_TIME)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCustomBigContentView(notificationLayout)
.setCustomContentView(notificationLayoutExpanded)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(fullScreenPendingIntent)
.setFullScreenIntent(fullScreenPendingIntent, true);
if (Build.VERSION.SDK_INT < 26) {
builder.setPriority(NotificationCompat.PRIORITY_MAX);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createCallNotificationChannel();
}
notificationManager.notify(notificationId, builder.build());
}
And i call it like this
onMessageReceived()
if (Build.VERSION.SDK_INT > 28) {
if (isAppOnForeground(getApplicationContext())) {
sendBroadcast(remoteMessage);
} else {
showCallNotification(dataMap);
}
} else {
sendBroadcast(remoteMessage);
}
Related
I'm trying to create an android full screen notification to show an activity over the lock screen, like an alarm clock.
The notification always occurs, but the activity is never started over the lock screen; it just rings and shows a notification icon on the lock screen if the phone is off. It shows a heads up notification if the phone is on as expected. A debug print indicates the notification channel is successfully registered at importance level HIGH/4 as requested.
I've tried it on 5 different android device versions: Android 10, 8.0.0, 6.0.1, 5.1.1
I've followed the android developers documentation linked below. I also linked a couple similar stack overflow questions.
https://developer.android.com/training/notify-user/time-sensitive
https://developer.android.com/training/notify-user/build-notification#urgent-message
Full screen intent not starting the activity but do show a notification on android 10
FullScreen Notification
Below is a very minimal version of the application code, an activity with 1 button to schedule the notification in the future with a broadcast receiver so it fires after the screen is locked.
compileSdkVersion 29
buildToolsVersion "29.0.2"
minSdkVersion 25
targetSdkVersion 29
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
public class AppReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (FullscreenActivity.FULL_SCREEN_ACTION.equals(intent.getAction()))
FullscreenActivity.CreateFullScreenNotification(context);
}
}
public class FullscreenActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "my_channel";
static final String FULL_SCREEN_ACTION = "FullScreenAction";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
createNotificationChannel(this);
}
/**
* Use button to set alarm manager with a pending intent to create the full screen notification
* after use has time to shut off device to test with the lock screen showing
*/
public void buttonClick(View view) {
Intent intent = new Intent(this, AppReceiver.class);
intent.setAction(FULL_SCREEN_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if (am != null) {
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15000, pi);
}
}
static void CreateFullScreenNotification(Context context) {
Intent fullScreenIntent = new Intent(context, FullscreenActivity.class);
fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//?
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Full Screen Alarm Test")
.setContentText("This is a test")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setDefaults(NotificationCompat.DEFAULT_ALL) //?
.setFullScreenIntent(fullScreenPendingIntent, true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(1, notificationBuilder.build());
}
private static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (notificationManager != null && notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("channel_description");
notificationManager.createNotificationChannel(channel);
}
//DEBUG print registered channel importance
if (notificationManager != null && notificationManager.getNotificationChannel(CHANNEL_ID) != null) {
Log.d("FullScreenActivity", "notification channel importance is " + notificationManager.getNotificationChannel(CHANNEL_ID).getImportance());
}
}
}
}
I was finally able to get this to work after finding this answer for an incoming call:
https://stackoverflow.com/a/53192049/13008865
The part missing from the android document examples for full screen intents was that the activity the full screen intent tries to show needs a couple WindowManager.LayoutParams flags set:
FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON.
Here's the final minimal test app code I hope is useful for others trying to do an alarm clock type app. I tested successfully on the 4 OS versions listed above with target sdk 29 and minimum sdk 15. The only manifest permission needed was USE_FULL_SCREEN_INTENT and only for devices running android Q/29 and above.
public class AppReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (FullscreenActivity.FULL_SCREEN_ACTION.equals(intent.getAction()))
FullscreenActivity.CreateFullScreenNotification(context);
}
}
public class FullscreenActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "my_channel";
static final String FULL_SCREEN_ACTION = "full_screen_action";
static final int NOTIFICATION_ID = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
createNotificationChannel(this);
//set flags so activity is showed when phone is off (on lock screen)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
/**
* Use button to set alarm manager with a pending intent to create the full screen notification
* after use has time to shut off device to test with the lock screen showing
*/
public void buttonClick(View view) {
Intent intent = new Intent(FULL_SCREEN_ACTION, null, this, AppReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 15000, pendingIntent);
}
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID); //cancel last notification for repeated tests
}
static void CreateFullScreenNotification(Context context) {
Intent intent = new Intent(context, FullscreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Full Screen Alarm Test")
.setContentText("This is a test")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true);
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notificationBuilder.build());
}
private static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("channel_description");
notificationManager.createNotificationChannel(channel);
}
}
}
}
//use the following code it will work
//also put this in your Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"/>
//put this in manifest in your specific activity you want to show on lock
//screen
android:showWhenLocked="true"
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(AccessibilityEventCompat.TYPE_WINDOWS_CHANGED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
As per other answers posted here, the two flags FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON are crucial for Android version above OREO.
However seems like the docs suggests it is better to declare them in AndroidManifest for that activity instead:
When using the Window flag during activity startup, there may not be time
to add it before the system stops your activity for being behind the
lock-screen. This leads to a double life-cycle as it is then restarted.
For me, instead of adding the flags programmatically, adding them in AndroidManifest is cleaner anyway, and it works well in my testing
<activity
android:name=".IncomingCallActivity"
android:showForAllUsers="true"
android:showWhenLocked="true"
android:turnScreenOn="true"
android:theme="#style/AppTheme" />
Not sure if showForAllUsers is needed, but I read somewhere that it is better to include it.
For me, what helped was Ranjith Kumar's answer in this question.
Below is the same code, in Java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
// For newer than Android Oreo: call setShowWhenLocked, setTurnScreenOn
setShowWhenLocked(true);
setTurnScreenOn(true);
// If you want to display the keyguard to prompt the user to unlock the phone:
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
} else {
// For older versions, do it as you did before.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
I also had a similar issue, for me the problem was not with window Flags but when triggering notification both the notification channel should have IMPORTANCE_HIGH and notification with HIGH_PRIORITY and also of you are using redmi you have to set permission explicitly on app settings
im stuck trying to show an activity from notification in secure lockscreen (in not secure lockscreen I achieve this).
I follow some questions an answers from StackOverflow but none resolve my problem.
I will post parts of my code.
MainActivity (activity)
This is executed in onCreate()
Intent intentService = new Intent(this,NotificationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intentService);
} else {
startService(intentService);
}
NotificationService (service)
This is executed in onStartCommand (tried to in onCreate)
public void crearNotificacion(){
RemoteViews rmv = new RemoteViews(getPackageName(),R.layout.notification_custom);
/*Intent intent = new Intent(this, wPerfil.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, 54, intent,PendingIntent.FLAG_UPDATE_CURRENT);*/
Intent intent = new Intent(this,NotificationIntentService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getService(this,54,intent,
PendingIntent.FLAG_UPDATE_CURRENT);
createNotificationChannel();// Este trozo de codigo es para versiones +26
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(rmv)
.setSmallIcon(R.drawable.ic_trasnparent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setContentIntent(pendingIntent)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setShowWhen(false)
.setGroupSummary(false)
.setAutoCancel(true);
startForeground(intentID,builder.build());
}
// CODIGO DE LA DOC DE ANDROID para versiones +26
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notificacion +26";
String description = "Notificacion para 26+";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
NotificationIntentService (IntentService)
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
handleNotfication();
}
}
private void handleNotfication(){
Intent intent = new Intent(this,wPerfil.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
ClassWhatIWantToLoad (activity)
This is executed in the onCreate
if (Build.VERSION.SDK_INT >= 27) {
setShowWhenLocked(true);
setTurnScreenOn(true);
}else{
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Permissions gived in Manifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
Hope this parts of code help you to understand my problem. I read in some other questions people achieve this doing the same I do, but i cant. What is wrong? If you have another solution like a widget that I can open in lockScreen tell me please.
Try deleting from the notification creation
.setContentIntent(pendingIntent)
And instead add
rmv.setOnClickPendingIntent(R.id.layoutID, pendingIntent);
before you set the customview on the notification. R.id.layoutID should be the ID of the relativelayout for your custom notification layout.
I updated my OS version to android 10 last night, and since then the startActivity function inside the broadcast receiver is doing nothing. This is how I try to start the activity based on the answer of CommonsWare:
Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
Log.d("Debug", "This is android 10");
// Start the alert via full-screen intent.
PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "my_channel_02";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.notification_channel_name_second),
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(startAlarmPendingIntent, true);
Log.d("Debug", "Try to load screen");
notificationManager.notify(0, builder.build());
}
The log shows that I am getting to the notify command but nothing happens. I am asking for USE_FULL_SCREEN_INTENT permission on the manifest so I should be able to use full-screen intents.
My app is useless now because of that issue. Does anyone know how to solve it?
Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.
Use a high-priority notification, with an associated full-screen Intent, instead. See the documentation. This sample app demonstrates this, by using WorkManager to trigger a background event needing to alert the user. There, I use a high-priority notification instead of starting the activity directly:
val pi = PendingIntent.getActivity(
appContext,
0,
Intent(appContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(pi, true)
val mgr = appContext.getSystemService(NotificationManager::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
) {
mgr.createNotificationChannel(
NotificationChannel(
CHANNEL_WHATEVER,
"Whatever",
NotificationManager.IMPORTANCE_HIGH
)
)
}
mgr.notify(NOTIF_ID, builder.build())
You can use SYSTEM_ALERT_WINDOW to force launch activity window in android 10, refer to this settingsuperposition setting:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<activity
android:name=".MainActivity"
android:label="#string/app_name"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".OnBootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
in launched app check permissions:
private void RequestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
} else {
//Permission Granted-System will work
}
}
}
you will can user intent as android older versions
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
try {
Intent activity = new Intent(context, MainActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
} catch (Exception e){
Log.d(TAG,e.getMessage()+"");
}
}
}
Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.
So you need to have a high-level notification and when the user clicks on the notification your activity will be opened notifications
public class UIExampleReceiver extends BroadcastReceiver {
public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
public static final String CHANNEL_ID = "channel_1111";
public static final int NOTIFICATION_ID = 111111;
private static final String TAG = "Receiver";
#Override
public void onReceive(Context context, Intent intent) {
try {
// If android 10 or higher
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
{
startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
}
else
{
// If lower than Android 10, we use the normal method ever.
Intent activity = new Intent(context, ExampleActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
} catch (Exception e)
{
Log.d(TAG,e.getMessage()+"");
}
}
// notification method to support opening activities on Android 10
public static void startActivityNotification(Context context, int notificationID,
String title, String message) {
NotificationManager mNotificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Create GPSNotification builder
NotificationCompat.Builder mBuilder;
//Initialise ContentIntent
Intent ContentIntent = new Intent(context, ExampleActivity.class);
ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
0,
ContentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setColor(context.getResources().getColor(R.color.colorPrimaryDark))
.setAutoCancel(true)
.setContentIntent(ContentPendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
"Activity Opening Notification",
NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setDescription("Activity opening notification");
mBuilder.setChannelId(CHANNEL_ID);
Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
}
Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID,
mBuilder.build());
}
}
I updated my OS version to android 10 last night, and since then the startActivity function inside the broadcast receiver is doing nothing. This is how I try to start the activity based on the answer of CommonsWare:
Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
Log.d("Debug", "This is android 10");
// Start the alert via full-screen intent.
PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "my_channel_02";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.notification_channel_name_second),
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(startAlarmPendingIntent, true);
Log.d("Debug", "Try to load screen");
notificationManager.notify(0, builder.build());
}
The log shows that I am getting to the notify command but nothing happens. I am asking for USE_FULL_SCREEN_INTENT permission on the manifest so I should be able to use full-screen intents.
My app is useless now because of that issue. Does anyone know how to solve it?
Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.
Use a high-priority notification, with an associated full-screen Intent, instead. See the documentation. This sample app demonstrates this, by using WorkManager to trigger a background event needing to alert the user. There, I use a high-priority notification instead of starting the activity directly:
val pi = PendingIntent.getActivity(
appContext,
0,
Intent(appContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(pi, true)
val mgr = appContext.getSystemService(NotificationManager::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
) {
mgr.createNotificationChannel(
NotificationChannel(
CHANNEL_WHATEVER,
"Whatever",
NotificationManager.IMPORTANCE_HIGH
)
)
}
mgr.notify(NOTIF_ID, builder.build())
You can use SYSTEM_ALERT_WINDOW to force launch activity window in android 10, refer to this settingsuperposition setting:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<activity
android:name=".MainActivity"
android:label="#string/app_name"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".OnBootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
in launched app check permissions:
private void RequestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
} else {
//Permission Granted-System will work
}
}
}
you will can user intent as android older versions
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
try {
Intent activity = new Intent(context, MainActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
} catch (Exception e){
Log.d(TAG,e.getMessage()+"");
}
}
}
Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.
So you need to have a high-level notification and when the user clicks on the notification your activity will be opened notifications
public class UIExampleReceiver extends BroadcastReceiver {
public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
public static final String CHANNEL_ID = "channel_1111";
public static final int NOTIFICATION_ID = 111111;
private static final String TAG = "Receiver";
#Override
public void onReceive(Context context, Intent intent) {
try {
// If android 10 or higher
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
{
startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
}
else
{
// If lower than Android 10, we use the normal method ever.
Intent activity = new Intent(context, ExampleActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
} catch (Exception e)
{
Log.d(TAG,e.getMessage()+"");
}
}
// notification method to support opening activities on Android 10
public static void startActivityNotification(Context context, int notificationID,
String title, String message) {
NotificationManager mNotificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Create GPSNotification builder
NotificationCompat.Builder mBuilder;
//Initialise ContentIntent
Intent ContentIntent = new Intent(context, ExampleActivity.class);
ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
0,
ContentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setColor(context.getResources().getColor(R.color.colorPrimaryDark))
.setAutoCancel(true)
.setContentIntent(ContentPendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
"Activity Opening Notification",
NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setDescription("Activity opening notification");
mBuilder.setChannelId(CHANNEL_ID);
Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
}
Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID,
mBuilder.build());
}
}
How do I start an activity over the lock screen after clicking on a notification? Whatever I do it asks for my password and starts the activity after I unlock. It doesn't even get to the onCreate methods.
<activity
android:name=".Locktask"
android:configChanges="orientation"
android:excludeFromRecents="true"
android:noHistory="true"
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:taskAffinity=""
android:exported="true"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar" />
Intent intent = new Intent(this, Locktask.class);
PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("locknote",
"locknote name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("shows layout over lock.");
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setVibrationPattern(new long[] { 0,0 });
channel.enableVibration(false);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(channel);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "locknote")
.setContentTitle("w")
.setContentText("e")
.setContentIntent(pi)
.setSmallIcon(R.drawable.ic_action_lock_notification)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(2, mBuilder.build());
Not a duplicate as that answer's permissions have already been added.
Edit: SOVLED
For any poor souls in the future, the notification has to have a custom layout with setCustomContentView() and a onclickpendingintent set to that view. The pending intent should point to an intent service, it's the only way intents can get handled from notifications while still locked.
Try this:
Intent intent = new Intent(this, Locktask.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);