Android How to - Use PendingIntent to create Notification








The following code shows how to Use PendingIntent to create Notification.

Example

Add activity to the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest...
    <application android:label="@string/app_name">

        <activity android:name=".SecondActivity" android:label="@string/app_name">
        </activity>
    </application>
</manifest>

Layout for main activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <Button android:id="@+id/notify"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"
      android:text="Click to raise a notification in 5 seconds"
      />
  <Button android:id="@+id/cancel"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"
      android:text="Click to clear the notification"
      />
</LinearLayout>

Java code

/***/*from  ww  w  . j  a  v  a 2s .c  om*/
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
  private static final int NOTIFY_ME_ID=1337;
  private Timer timer=new Timer();
  private int count=0;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    Button btn=(Button)findViewById(R.id.notify);
    
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        TimerTask task=new TimerTask() {
          public void run() {
            notifyMe();
          }
        };
        timer.schedule(task, 5000);
      }
    });
    
    btn=(Button)findViewById(R.id.cancel);
    
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        mgr.cancel(NOTIFY_ME_ID);
      }
    });
  }
  
  private void notifyMe() {
    final NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification note=new Notification(R.drawable.red_ball,"Status message!",System.currentTimeMillis());
    PendingIntent i=PendingIntent.getActivity(this, 0,new Intent(this, SecondActivity.class),0);
    note.setLatestEventInfo(this, "Notification Title","This is the notification message", i);
    note.number=++count;
    
    mgr.notify(NOTIFY_ME_ID, note);
  }
}

Second activity

/***//from   ww w  .  j a va 2s .c  o  m
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    TextView txt=new TextView(this);
    
    txt.setText("This is the message!");
    setContentView(txt);
  }
}