Reputation: 679
I try to launch activity for a broadcastReceiver by using the next code
Intent i = new Intent(context, AlarmNotification.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...
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
"Whatever", NotificationManager.IMPORTANCE_HIGH));
}
mgr.notify(NOTIFY_ID, buildNormal(context, i).build());
}
private NotificationCompat.Builder buildNormal(Context context, Intent intent) {
NotificationCompat.Builder b=
new NotificationCompat.Builder(context, CHANNEL_WHATEVER);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(TEXT)
.setContentText(TEXT)
.setFullScreenIntent(buildPendingIntent(context, intent), true);
return(b);
}
private PendingIntent buildPendingIntent(Context context, Intent intent) {
return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
In the beginning, everything work's perfectly fine. But if I enter the app settings, turn off the notification channel of CHANNEL_WHATEVER, and then turn it on again. Later when I call NotificationManager.notify it shows the notification in the notification drawer but does not start the activity. If I delete the app and reinstall, it works fine again. Is that a bug of android 10 which I should report on, or there is something I can do about it?
Upvotes: 17
Views: 22809
Reputation: 35
for these who has problems with:
builder.setFullScreenIntent(pendingIntent, true)
please try to use instead:
builder.setContentIntent(pendingIntent)
Upvotes: -2
Reputation: 2437
In my case the problem was in the notification id which is used to show the full screen intent notification.
If we have the previous notification with the same notification id, then the full screen intent activity will not be launched instead heads up notification will be shown in the lock screen.
so try using the unique notification id for the full screen intent notification. For more info about how to setup full screen intent notification:Display time-sensitive notifications
Upvotes: 0
Reputation: 1
You should wake up the screen of Android, before showing the notification:
fun asquireWake() {
val mPowerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val mWakeLock: PowerManager.WakeLock = mPowerManager.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
"YourApp:Whatever"
)
mWakeLock.acquire()
}
Upvotes: 0
Reputation: 21
If you want to display your own activity on lock screen you need to enable showOnLockScreen in Manifest
<activity android:name="NewActivity" android:showOnLockScreen="true" />
and set flag showWhenLocked in your activity class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
}
}
Finally create a fullScreenIntent with your activity class and attach it to notification
Intent fullScreenIntent = new Intent(applicationContext, NewActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(applicationContext, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(applicationContext, channel)
.setSmallIcon(R.drawable.ic_small)
.setContentTitle(textTitle)
.setContentText(textContent)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setFullScreenIntent(fullScreenPendingIntent, true)
.setAutoCancel(true);
Upvotes: 2
Reputation: 43
not sure if this was answered. In addition to Permissions previously mentioned... The only way i found to make this work is to use a "NotificationManager.IMPORTANCE_HIGH" Channel and the Notification. After setting to HIGH, the fullScreenIntent will be opened if the screen is off. Changing the Channel later seems to require an uninstall and reinstall.
Set up the Notification Channel before sending the Notification as (see also):
private static void createNotificationChannel(Context context) {
// 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 = **"YOUR CHANNEL"**;
String description = "YOUR CHANNEL DESCRIPTION";
int importance = **NotificationManager.IMPORTANCE_HIGH**;
NotificationChannel channel = new NotificationChannel( "YOUR 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 = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
I set both intents, unclear if you have to set both:
createNotificationChannel((context));
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, **"YOUR CHANNEL**");
builder.setContentIntent(pendingIntent);
builder.setFullScreenIntent(pendingIntent,true);
Upvotes: 2
Reputation: 22173
Android doesn't grant that the activity is shown even if you use a full screen intent:
On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
Upvotes: 4
Reputation: 69671
In Android 10 we need to add permission for USE_FULL_SCREEN_INTENT
Permissions changes for fullscreen intents
Apps that target Android 10 or higher and use notifications with fullscreen intents must request the USE_FULL_SCREEN_INTENT
permission in their app's manifest file.
This is a normal permission, so the system automatically grants it to the requesting app.
Make sure you have added permission in manifest file
SAMPLE CODE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nilu.demo">
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 27