Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Notification with BroadcastReceiver

I tried to create a Notification with this Code:

private void setNotificationAlarm(Context context) 
{
    Intent intent = new Intent(getApplicationContext() , MyNotification.class);
    PendingIntent pendingIntent  = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);  

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 , pendingIntent);
    Log.d("ME", "Alarm started");
}

public class MyNotification extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("ME", "Notification started");

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

And here my Mainfest declaration:

<receiver
    android:name=".MyNotification"
    android:enabled="true"
    android:exported="false" >
</receiver>

My problem now is, that the alarm is generated but the Notification isn't displayed. The BroadcastReceiver is declared in the mainfest file and there are no compiler or runtime errors.

My second problem is that setLatestEventInfo and new Notification Contructor are deprecated. What can I use instead of it?

like image 370
Cilenco Avatar asked May 10 '13 22:05

Cilenco


People also ask

How do you notify an activity from a BroadcastReceiver?

1 Answer. Show activity on this post. The broadcast is the notification. :) If you want to say, start an activity or a service, etc., based on a received broadcast then you need a standalone broadcast receiver and you put that in your manifest file.

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

How to create Android notification with broadcastreceiver?

This example demonstrate about How to create Android Notification with BroadcastReceiver. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to res/menu/main_menu.xml.

How to make broadcastreceiver works for the system broadcasted intents?

There are following two important steps to make BroadcastReceiver works for the system broadcasted intents − Creating the Broadcast Receiver. There is one additional steps in case you are going to implement your custom intents then you will have to create and broadcast those intents.

How do I create a broadcast receiver in Xamarin Android?

To create a broadcast receiver in Xamarin.Android, an application should subclass the BroadcastReceiverclass, adorn it with the BroadcastReceiverAttribute, and override the OnReceivemethod:

How do I register a broadcast receiver?

Context-Registering a Broadcast Receiver Context-registration (also referred to as dynamic registration) of a receiver is performed by invoking the RegisterReceivermethod, and the broadcast receiver must be unregistered with a call to the UnregisterReceivermethod.


1 Answers

I think you need to use

PendingIntent.getBroadcast (Context context, int requestCode, Intent intent, int flags)

instead of getService

like image 82
Ashwini Bhangi Avatar answered Oct 19 '22 10:10

Ashwini Bhangi