Reputation: 642
Hi in my project i'm showing notification with two button 1 is for syn and another one is for close. I have used PendingIntent to get the data from the notification but when i click the any button it giving a same value plz help me on this
there is my coding
@SuppressWarnings("deprecation")
private void startNotification(){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager =
(NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null,
System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.number);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(this, switchButtonListener.class);
switchIntent.putExtra("do_action", "close");
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.close,
pendingSwitchIntent);
Intent switchIntentsyn = new Intent(this, switchButtonListener.class);
switchIntentsyn.putExtra("do_action", "syn");
PendingIntent pendingSwitchIntentsyn = PendingIntent.getBroadcast(this, 0,
switchIntentsyn, 0);
notificationView.setOnClickPendingIntent(R.id.startsyn,
pendingSwitchIntentsyn);
notificationManager.notify(100, notification);
}
this is my xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="Title name"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="@+id/startsyn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Syn now" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="close" />
</LinearLayout>
</LinearLayout>
this my BroadcastReceiver
public static class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = (String) intent.getExtras().get("do_action");
Log.i("tag", "action :"+action);
if (action != null) {
if (action.equalsIgnoreCase("syn")) {
// for example play a music
Log.i("tag", "inside syn method");
} else if (action.equalsIgnoreCase("close")) {
// close current notification
Log.i("tag", "inside close method");
}
}
}
}
this is my android manifest
<receiver android:name="com.example.testdemo.MainActivity$switchButtonListener" />
my out put log is same when ever i click both button
08-27 19:35:01.347: I/tag(8547): action :close
08-27 19:35:01.347: I/tag(8547): inside close method
Upvotes: 0
Views: 1213
Reputation: 689
This is because you are probably passing the same intent ids to both the pending Intent and getting a common broadcast for both the actions.
Change the ID in the following line to 1 PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
Like this PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 1, switchIntent, 0);
It should work fine now!
Upvotes: 2