Search code examples
androidbroadcastreceiver

Broadcast receiver not posting my data


I am trying to implement broadcast receiver on notification button. The problem is I do not see a toast.

Funtion for custom notification with button, on button click should be sended notification:

public void CustomNotification() {

    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.customnotification);

    String strtitle = getString(R.string.customnotificationtitle);
    String strtext = getString(R.string.customnotificationtext);
    Intent intent = new Intent(this, NotificationView.class);
    intent.putExtra("title", strtitle);
    intent.putExtra("text", strtext);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.desktop_icon)
            .setTicker(getString(R.string.customnotificationticker))
            .setAutoCancel(true)
            .setContentIntent(pIntent)
            .setContent(remoteViews);


    remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
    remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.icon_connected_phone);
    remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
    remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext));

    //On button click
    Intent BRintent = new Intent("MyCustomIntent");
    BRintent.putExtra("message", "TST");
    BRintent.setAction("com.example.notificationtest.CUSTOM_INTENT");
    sendBroadcast(intent);

    PendingIntent pIntent2 = PendingIntent.getActivity(this, 0, BRintent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.notif_btn, pIntent2);
    //End on button click

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationmanager.notify(0, builder.build());

}

On manifest added:

 <receiver android:name="MyBroadcastReceiver">
            <intent-filter>
                    <action android:name="com.example.notificationtest.CUSTOM_INTENT">
                    </action>
            </intent-filter>
 </receiver>

Broadcast receiver class:

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        // Extract data included in the Intent
        CharSequence intentData = intent.getCharSequenceExtra("message");   
        Toast.makeText(context, "Test Notification"+intentData, Toast.LENGTH_LONG).show();
        Log.d("MyTag", "Test");
    }

}

Solution

  • In this call:

    PendingIntent pIntent2 = PendingIntent.getActivity(this, 0, BRintent, 
             PendingIntent.FLAG_UPDATE_CURRENT);
    

    You are calling getActivity(), which will return you a PendingIntent that starts an Activity. That isn't what you want. You want a broadcast Intent. Do this instead:

    PendingIntent pIntent2 = PendingIntent.getBroadcast(this, 0, BRintent, 
             PendingIntent.FLAG_UPDATE_CURRENT);