I have a single Activity
in my app (called MainActivity) and when I send notification to user, I want it to get opened if the notification is clicked. the important point is to avoid activity recreation and if there is already an instance of it alive, bring it to front and deliver the data to it. to get this behavior I have tried:
Intent intent = new Intent(getApplicationContext(), MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
.putExtra("data", ...);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
and in manifest:
<activity
android:name="...activities.MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
activity>
but the onNewIntent(Intent intent)
method never gets called when I touch the notification, instead onCreate
is called and the activity is recreated. please note that since I'm testing, I send the notification from the MainActivity itself and while it is open, I drag status bar and click on my notification (which then recreates the MainActivity). what is the problem?
The code that you shared seems to be fine and only concern I see is setIntent()
inside onNewIntent()
.
May be you can try removing them and check. And next is getApplicationContext()
that you are passing in the PendingIntent
. Try passing it as MainActivity.this
.
Below is my snippet for reference. I done it in Kotlin
instead of Java
. However logic is same.
Manifest.xml
<activity
android:name = ".MainActivity"
android:launchMode = "singleTop">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Logic for generating notification residing in MainActivity
val intent = Intent(this, MainActivity::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
intent.putExtra("DATA", "SENT FROM PENDING INTENT")
val contentIntent = PendingIntent.getActivity(
applicationContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val b = NotificationCompat.Builder(this, getString(R.string.app_name))
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentIntent(contentIntent)
.setContentInfo("Info")
val nmanager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nmanager.notify(1, b.build())
And this is my onNewIntent()
part
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.i("onNewIntent", "Called")
intent?.let {
binder.msgView.text = it.getStringExtra("DATA")
Log.i("onNewIntent:", "Received new intent data")
}
}
NOTE : With the help of button action I generate the notification from inside the MainActivity
.