Reputation: 13545
I am now working on a notification reminder, if the user click on the notification , it will open the app.
However, the problem is , if I have already open the app, when I click on the notification it still create a new activity, so , there are two activity in total.
How can I achieve like this? If the user has already open the app , just do nothing, if not , then open the app. Thanks for helping.
public class AlarmService extends Service {
private static final int MY_NOTIFICATION_ID = 1;
NotificationManager notificationManager;
Notification myNotification;
Vibrator v;
public AlarmService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(5000);
Context context = getApplicationContext();
Intent myIntent = new Intent(this, SplashScreen.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, myIntent, Intent.FLAG_ACTIVITY_SINGLE_TOP);
myNotification = new Notification.Builder(context)
.setContentTitle(getResources().getString(R.string.notify_title))
.setContentText(getResources().getString(R.string.notify_msg) + "\n"
+ getResources().getString(R.string.reminder_1) + "aaa\n"
+ getResources().getString(R.string.reminder_2) + "vvv\n"
+ getResources().getString(R.string.reminder_3) + "cccc\n"
+ getResources().getString(R.string.reminder_5) + "dddd\n")
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false)
.setSmallIcon(R.drawable.ic_launcher).build();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
Upvotes: 1
Views: 1171
Reputation: 2140
Try below code.
public static void startNotification(Service service, String message) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
if(prefs.getBoolean("pref_NotificationDisplayed", true)){
// Creates an Ongoing Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);
Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intentBack);
NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
// Send Notification
Notification primaryNotification = mBuilder.build();
primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(10001,primaryNotification);
}
}
Thats it...
Upvotes: 1