I have a problem about activity created twice.
When the notification appears, the activity will appear because of the fullscreen intent. At this time, I press the home button and click the notification, and the activity is actually created again.
I tried to use singletop and it didn't work.
Log shows the same task, but the hash is different.
Log.e("Task", "task id: $taskId, hash: ${hashCode()}")
val pendingIntent = PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
}, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
val builder = NotificationCompat.Builder(context, "Alarm")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Title")
.setContentText("Content")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(pendingIntent, true)
Related
I have an Activity, which has a fragment. In that fragment, I launch a service witch I need to do some stuff in the background. When some condition come to true in the service, I create a push notification, where i specify to reopen the activity (and load the fragment) when the notification is clicked.
The problem is that when i click the notification, the activity is destroyed and recreated again, and on the onDestroy method of the activity, I shut down the service (i want to kill it when the user close the app), so the service is no more running anymore.
How can i prevent the activity to be destroyed and recreated but simply resumed (like moving my self through the multitasking)?
How i create the notification:
val intent = Intent(context, SplashScreenActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, FLAG_ACTIVITY_SINGLE_TOP)
val builder = NotificationCompat.Builder(
ApplicationServices.instance.applicationContext,
channelId
)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
``
Try this:
// Activity Intent
val intent = Intent(context, SplashScreenActivity::class.java).apply {
// Note: Add flag(s) for Activity here
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
}
// Pending Intent
val pendingIntent = PendingIntent.getActivity(
context,
// Note: Use a non-zero positive Integer here, it's good practice ;-)
System.currentTimeMillis().toInt(),
intent,
// Note: Add flag(s) for Pending Intent here
PendingIntent.FLAG_UPDATE_CURRENT
)
// Notification Builder
val builder = NotificationCompat.Builder(
ApplicationServices.instance.applicationContext,
channelId
)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Using Pending Intent
.setContentIntent(pendingIntent)
.setAutoCancel(true)
UPDATE!!!
I solved this problem. The problem was with remoteMessage.notification.let {
which is triggered only when app is in foreground state, thus I removed this part and it worked like a charm)
When I send push notification and when app is in background state notification is coming but whem I click it is not doing anything. But when app is in foreground, the click is taking to an app again.
Here is some code from FirebaseMessagingService
remoteMessage.notification.let {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val intent = Intent(this, MainActivity::class.java)
.putExtra(CLICK_ACTION, mapData.get("click_action"))
.putExtra(PAGE_TO_GO, mapData.get("pagetogo"))
.putExtra(NOTIFIATION_TITLE, mapData.get("title"))
.putExtra(NOTIFICATION_BODY, mapData.get("body"))
.putExtra(COMPANY_ID, mapData.get("company_id"))
.putExtra(DOCUMENT_ID, mapData.get("document_id"))
.putExtra(ORDER_ID, mapData.get("order_id"))
.putExtra(FROM_DATE_EXTRA, mapData.get("from_date"))
.putExtra(TO_DATE_EXTRA, mapData.get("to_date"))
.putExtra(LEDGER_VIEW_ID_EXTRA, mapData.get("id_for_ledge_view"))
.putExtra(IS_CREDIT_EXTRA, mapData.get("is_credit"))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notification = NotificationCompat.Builder(this, getString(R.string.channel_id))
.setSmallIcon(R.drawable.ic_baseline_notifications_24)
.setContentTitle(it?.title)
.setContentText(it?.body)
.setContentIntent(pendingIntent)
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(it?.body)
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
with(notificationManager) {
notify(NOTIFICATION_ID, notification.build())
}
I solved this problem. The problem was with remoteMessage.notification.let { which is triggered only when app is in foreground state, thus I removed this part and it worked like a charm)
I'm able to show notification that redirects to call activity when call is incoming and screen is unlocked, but that notification doesn't respond when screen lock is active. I see it, but if I click on it, no events are getting fired.
I use following code from https://developer.android.com/training/notify-user/time-sensitive
val fullScreenIntent = Intent(this, CallActivity::class.java)
fullScreenIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_USER_ACTION or Intent.FLAG_ACTIVITY_SINGLE_TOP
val fullScreenPendingIntent = PendingIntent.getActivity(
this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
val notificationBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setContentIntent(fullScreenPendingIntent)
.setFullScreenIntent(fullScreenPendingIntent, true)
val incomingCallNotification = notificationBuilder.build()
startForeground(NOTIFICATION_ID, incomingCallNotification)
Iadded permission:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
I've tried numerous suggestions on SO, but none of them seem to work. My activity does have required code to open above lock screen, but it's onCreate or onNewIntent newer get called from lockscreen.
Any ideas?
I have a notification and when I tap it I want to launch application if it is still not running, but if application already running, I do not want to relaunch it.
So, I am using PendingIntent.FLAG_UPDATE_CURRENT flag when creating PendingIntent.
My code:
private val notificationManager by lazy { NotificationManagerCompat.from(this) }
fun testPush() {
val notificationBuilder = NotificationCompat.Builder(this, BuildConfig.APPLICATION_ID)
.setSmallIcon(R.drawable.ill_launcher)
notificationBuilder
.setContentTitle("Title")
.setContentText("Test text")
.setContentIntent(buildPendingIntent())
notificationBuilder
.setAutoCancel(true)
.priority = NotificationCompat.PRIORITY_DEFAULT
notificationManager.notify(1, notificationBuilder.build())
}
private fun buildPendingIntent(): PendingIntent {
val intent = Intent(this, RootActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
intent.putExtra("action", RootActivity.DEFAULT_INTENT)
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
But when I launch the application and click on the notification, the activity is recreates.
Instead of building the Intent for RootActivity like you are doing, you need to create a "launch Intent" for your application. The easiest way to do this is to call PackageManager.getLaunchIntentForPackage() and pass your own package name. Use the returned Intent in the call to PendingIntent.getActivity().
This will launch your app if it isn't running, otherwise, if it is already running, it will just bring the existing task containing your app to the foreground.
I have tested the new BigTextStyle Notification which display correctly. However when I add action to the notification, it never get cancel and the pull-down notification area is never close when click on the action. What did I do wrong?
Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add);
Bitmap b = ((BitmapDrawable)d).getBitmap();
Notification noti = null;
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
String s = "Name: John \n"
+ "Surname: Doe\n"
+ "Vehicle: Honda Jazz TA-1234\n"
+ "Note: Good drifter";
Builder builder = new Notification.Builder(MainActivity.this)
.setContentTitle("New challenger arrived!")
.setContentText(s)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(b)
.setTicker("New challenger arrived!")
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setNumber(5)
.addAction(
android.R.drawable.ic_menu_add,
"Accept",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_menu_delete,
"Refuse",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_dialog_map,
"Map",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null));
noti = new Notification.BigTextStyle(builder)
.setBigContentTitle("New challenger arrived!")
.bigText(s)
.setSummaryText("You will never win against me").build();
NotificationManager nm = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
I think you have to manually cancel the notifications if you take an action. This can be done by calling
notifMgr.cancel(0);
Clicking the notification itself should dismiss it, but for some reason actions don't seem to. The reason you pass 0 as the argument is becasue that's the ID you gave when you called:
notifMgr.notify(0, noti);
That's my updated answer, however, I had previously answered this:
It looks like you're trying to launch the same activity (from your PendingIntent) that you currently have displayed (I'm assuming this is a 1-activity app, with MainActivity containing the code above). Create another activity, build your PendingIntent so it launches that activity, then it should do as you wish. Alternatively, run your activity, notification appears, navigate to homescreen, or different app, then act on your Activity, your desired behavior should manifest.