Reputation: 18171
I have function sendNotification
that creates notification:
public class MainActivity extends AppCompatActivity {
public Handler timerHandler = new Handler();
public Runnable timerRunnable = new Runnable() {
@Override
public void run()
{
//timerHandler.postDelayed(this, RECONNECT_TIMEOUT);
Timber.tag(Utils.TIMBER_TAG).v("Timer is working " );
//show("Connected to server "+mqttHelper.mqttAndroidClient.isConnected());
sendNotification("Hi", "Hello");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timber.plant(new Timber.DebugTree());
Timber.tag(Utils.TIMBER_TAG).v("onCreate" );
setContentView(R.layout.activity_main);
timerHandler.postDelayed(timerRunnable, 2000);
}
@Override
protected void onDestroy() {
super.onDestroy();
Timber.tag(Utils.TIMBER_TAG).v("onDestroy");
}
public void sendNotification(String title, String message) {
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setContentTitle(title)
.setTicker("ticker")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
In case I set PendingIntent
my application restarts when clicking on message:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
This is not acceptable. I need to show current running application and do not restart it.
In case I comment mentioned PendingIntent
code message is show, but clicking on it takes no action.
How to show MainActivity window when clicking on notification message?
UPD:
I found I have strange behavior in log file:
onCreate
Timer is working
onCreate
onCreate
onDestroy
onDestroy
Timer is working
Timer is working
I have two onCreate
and two onDestroy
. Why?
Upvotes: 1
Views: 42
Reputation: 1018
In AndroidManifest.xml, you add launchMode:"singleTask"
<activity
android:name=".MainActivity"
android:launchMode="singleTask"/>
Upvotes: 0
Reputation: 1184
Try this out:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity, 0, notificationIntent, 0);
Setting FLAG_ACTIVITY_NEW_TASK
should just bring an existing task for the application from the background to the foreground without actually creating an instance of the activity.
Upvotes: 1