user3737090
user3737090

Reputation:

how to open anoter activity if click on notification?

 public class SimpleService extends Service {
 private NotificationManager mNM;
 private int NOTIFICATION = 0;

 public class LocalBinder extends Binder {
        SimpleService getService() {
            return SimpleService.this;
        }
    }
 @Override
 public IBinder onBind(Intent intent) {
    return mBinder;
 }

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();        
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    mNM.cancel(NOTIFICATION);
    // Tell the user we stopped.
    Toast.makeText(this,"Service is destroy", Toast.LENGTH_SHORT).show();
}   

@Override
public void onStart(Intent intent, int startId) {
    super.onCreate();
    Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"task perform in service",300).show();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();
    return START_STICKY;
}   

  // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text,
                System.currentTimeMillis());
        Intent intent = new Intent(this,NotificationRecieverActivity.class);
        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, "latest information",
                       text, contentIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }

}

If I am click on notification then this notification is clear but I wont to show this notification message to another activity i.e NotificationRecieverActivity.class but this not display anything. Please help me.

Upvotes: 0

Views: 152

Answers (3)

iZBasit
iZBasit

Reputation: 1313

Do the following

 Intent intent = new Intent(this,NotificationRecieverActivity.class);
 intent.putExtra("YOURTAG", "DATA");
    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

OR Using bundle

Intent intent = new Intent(this,NotificationRecieverActivity.class);
Bundle bundle = new Bundle();
bundle.putString("YOURTAG", "DATA");
intent.putExtra("BUNDLETAG", bundle);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);

Full Class

public class GcmIntentService extends IntentService {
private NotificationManager mNotificationManager;

public GcmIntentService() {
    super("GcmIntentService");
}
public static final String TAG = "Mobien Reception Service";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that GCM will be
         * extended in the future with new message types, just ignore any message types you're
         * not interested in, or that you don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            makeMessage("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            makeMessage("Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else {
            Log.d(TAG, "Received Message :"+extras.getString("message"));
            makeMessage(extras.getString("message"));
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void makeMessage(String msg) {

    if(!msg.equals("") || msg.contains("#")) {
        String temp [] = StringUtility.split(msg, '#');
        String header = temp[0];
        Log.d(TAG, "Header Message :"+header);
        if(header.trim().contains("DLV")) {
            sendNotification("Del. No. "+temp[1], "Against SAP SO.No. "+temp[2], 123);
        } else if(header.trim().contains("PGI")) {
            sendNotification("PGI No. "+temp[1], "Against Del. No."+temp[2], 99);
        } else if(header.trim().contains("INV")) {
            sendNotification("Inv. No. "+temp[1], "Against Del. No."+temp[2], 157);
        }
    }
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message. 
private void sendNotification(String contentTitle, String contentText) {

    final Random r = new Random();
    final int notificationId = r.nextInt();

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LoginActivity.class), 0);

    Bitmap largeIcon= BitmapFactory.decodeResource(getApplicationContext().getResources(), 
            R.drawable.ic_launcher);

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(largeIcon)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setAutoCancel(true)
    .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));


    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(notificationId, mBuilder.build());
}

Its something I had made for my demo. I don't have time to modify according to your needs. But it should help you out in what you are doing.

Upvotes: 3

The_ehT
The_ehT

Reputation: 1230

Just add the pending intent you created, to the notification as -

notification.setContentIntent(contentIntent);

after creation of PendingIntent.

Upvotes: 0

MH2K9
MH2K9

Reputation: 12039

Try by intent something like below

Intent a=new Intent(this,NewActivity.class);
a.putExtra("var", "your message"); //value passing
startActivity(a);

And get the message in NewActivity class such as

try {
     Intent i = getIntent();            
     String message = i.getStringExtra("var");          
}catch (Exception ex) {
    Log.e("Error", "Loading exception");
}

Upvotes: 0

Related Questions