Christian Escobar F
Christian Escobar F

Reputation: 27

Creating a notification with BroadcastReceiver and AlarmManager

I am trying to create a notification using AlarmManager and BroadcastReceiver. ( API 19, KitkKat)

I am following steps tutorial https://www.youtube.com/watch?v=gm5n_hRIR-c (Starts at 10:51)

But I can not get the application to work. No throw notification. :/ Help, here is my code.

My MainActivity

public class MainActivity extends AppCompatActivity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Button btnN = ( Button) findViewById(R.id.btnalarm);
btnN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
 Long alertOfTime = new GregorianCalendar().getTimeInMillis() + 5 * 1000;

    Intent alertIntent = new Intent(this, Alert.class);

    AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    aManager.set(AlarmManager.RTC_WAKEUP,alertOfTime,
            PendingIntent.getBroadcast(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));

       }
    });
}

My class Alert

public class Alert extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

          createNotification (context,"NATURE TIP","When","Alert");

}

 public void createNotification (Context context ,String msgg ,String msgAlert,String msgText){
PendingIntent pendIntentTwo = PendingIntent.getActivity(context,0,
            new Intent(context,MainActivity.class),0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.tree_24_24)
            .setContentTitle(msgg)
            .setTicker(msgAlert)
            .setContentText(msgText);

 mBuilder.setContentIntent(pendIntentTwo);

    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);

    mBuilder.setAutoCancel(true);

NotificationManager nNotifManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);//

    nNotifManager.notify(1,mBuilder.build());

}
}

Mi MainLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@drawable/backk">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Alarm Notif"
    android:id="@+id/btnalarm"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>

and my Manifiest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="com.android.alarm.permission.SET_ALARMY"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="App Nature"
    android:theme="@style/Theme.AppCompat.Light"
    >
 <activity
        android:name=".MainActivity"
        android:label="App Nature" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>


</manifest>

Upvotes: 1

Views: 1905

Answers (1)

Developer_vaibhav
Developer_vaibhav

Reputation: 238

You didn't register the broadcast in your manifest. That's why it is not working . Broadcasts must be registered before use. So first register your class alert in your manifest and if you are using any action then also declare action in you manifest file using intent filter.

Upvotes: 1

Related Questions