Friday, March 23, 2012

Showing notificaiton, passing parameter from Intent to activity

//my activity is under com.hello
//my activity class name is HelloAndroidActivity

In your IntentService class or Service class do following:

private void showNotification() {

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.icon;
        CharSequence tickerText = "some ticket text";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "Some title";
        CharSequence contentText = "Some content";
        Intent notificationIntent = new Intent(this, HelloAndroidActivity.class);
        notificationIntent.setAction("com.hello"+System.currentTimeMillis());//notice : com.hello
        notificationIntent.putExtra("extra0", "some xtra message");
        notificationIntent.putExtra("extra1", "extra message");
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);//where 1 is the id,, different id will have different notification
    in your activity class override onNewIntent method as shown below for example
    public void onNewIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        String extra0 = extras.getString("extra0");
        String extra1 = extras.getString("extra1");

No comments:

Post a Comment