kobbycoder kobbycoder - 2 months ago 12
Android Question

How do I update Activity with intents passed via Notification

I have created a custom notification, but when i pause the activity with the home button and a notification arrives, i press the notification and it creates a new activity and doesnt resume the previews activity and when i press the back button it goes to the previews one which is the same window. I have tried

singleTop, singleTask, singleIntent
, it works but it doesnt update the activity when a message enters, it is like the previews activity was in pause. how can i fix it?

if there is no solution i though in resuming the activity or destroy the paused activity or maybe restart the activity, is there a way to do this?

public void CustomNotification(String strtext) {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);

// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
intent.putExtra("String T", "");
//intent.putExtra("Integer C", 0);

// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.drawable.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);

// Locate and set the Image into customnotificationtext.xml ImageViews
remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);

// Locate and set the Text into customnotificationtext.xml TextViews
remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, strtext);

// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());

}


LOGS:

04-29 01:29:10.453 29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376 29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981 29501-29501/com.example.example E/STATE﹕ RESUMING


WHEN I PRESS THE NOTIFICATION:

04-29 01:33:09.654 33449-33530/com.example.example E/STATE﹕NEW ACTIVITY


I will appreciate any answer, Thanks!

EDITED

ANSWER:

the answer was to add:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);


because when i was in the same window and i pressed the home button, the activity was in paused, then if some notification arrived it brought me to that window but didnt updated the messages so I needed to create a new intent and erase the previous one which was in pause. so that code did the Job.

Answer

try this in your service code to use notification :

 private static final int NOTIFY_ME_ID = 1222;
   NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

     private void Notifi(String message){
        int requestID = (int) System.currentTimeMillis();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                //CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("packagenameofyourapplication",
                        "packagenameofyourapplication.NotificationView"));
                PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Notification notif = new Notification.Builder(this)
                        .setContentTitle(getString(R.string.notification_titel)).setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
                        .setContentIntent(pintent).build();
                notif.flags = Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFY_ME_ID, notif);

}