Pages

Monday, July 23, 2012

Send Data when Clicking on a Notification in Android?

Now today I am going to show how to send data when you click on the notification message.
Here I am creating two activities. One the main activity that creates the notification and the other one is used when you click on the notification.
i.e the second activity is called when you click on the notification.
Here is the main activity that sends the notification.
NotificationDemo .java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.coderzheaven.pack;
 
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class NotificationDemo extends Activity implements View.OnClickListener{
    private Button buttonSend, clear;
    private static final int NOT_ID = 1;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      buttonSend = (Button) this.findViewById( R.id.send);
      clear = (Button) this.findViewById( R.id.cancel);
      buttonSend.setOnClickListener(this);
      clear.setOnClickListener(this);
 
    }
 
    @Override
    public void onClick(View v) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
 
        if(v == buttonSend){
            int icon = R.drawable.icon;
            CharSequence tickerText = "Hello ";
            long when = System.currentTimeMillis();
 
            int requestID = (int) System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);
            Context context = getApplicationContext();
            Intent notificationIntent = new Intent(this, GoToNotification.class);
            notificationIntent.putExtra("data1", "My Data 1");
            notificationIntent.putExtra("data2", "My Data 2");
            notificationIntent.setAction("myString"+ requestID);
            PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent, 0);
            notificationIntent.setData((Uri.parse("mystring"+requestID)));
                    notification.setLatestEventInfo(context, "Notification Demo", requestID + "", contentIntent);
                    notification.flags += Notification.FLAG_ONGOING_EVENT;
                    notification.flags += Notification.FLAG_AUTO_CANCEL;
                mNotificationManager.notify(NOT_ID, notification);
        }else{
            Toast.makeText(getApplicationContext(),"Cancelling", Toast.LENGTH_SHORT).show();
            mNotificationManager.cancel(NOT_ID);
        }
 
    }
}
The main.xml that creates the layout for the above activity.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
    android:text="Send Notification"
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
<Button
    android:text="Cancel Notification"
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>
Now the second activity that is called when you click on the notification.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.coderzheaven.pack;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class GoToNotification extends Activity{
 
    TextView tv = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate");
 
        setContentView(R.layout.notification);
        tv = (TextView)findViewById(R.id.tv);
 
        Bundle extras = getIntent().getExtras();
 
        if(extras != null){
            String data1 = extras.getString("data1");
            String data2 = extras.getString("data2");
            System.out.println("Ddata1 : " + data1);
            tv.setText("Data Sent from Clicking Notification nData 1 : " + data1 + "nData 2 : " + data2);
        }
    }
 
}
This is the layout for the second activity.
It simply contains a textview to show the passed data.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:id="@+id/tv"
    />
 
</LinearLayout>
Also declare the activity in the manifest.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".NotificationDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
     <activity android:name=".GoToNotification"
                  android:label="@string/app_name" />
    </application>
</manifest>




No comments:

Post a Comment

Popular Posts