Search code examples
androidandroid-manifestandroid-notificationsandroid-alarms

Android - alarm notification issue


I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.

Here is the creation of the alarm:

myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);

PendingIntent pendingIntent =
        PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(t.getDueDate().getTime());

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

In addition I've created: NotificationReceiver

import android.app.Activity;
import android.os.Bundle;

public class NotificationReceiver  extends Activity
{
 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
}

ReminderNotification

import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;

public class ReminderNotification extends BroadcastReceiver
{
    public ReminderNotification()
    {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // The PendingIntent to launch our activity if the user selects this notification
        Task task = (Task)intent.getSerializableExtra("task");
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
        // Set the info for the views that show in the notification panel.

        Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
        snzInt.putExtra("task", task);
        PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);

        Intent doneInt = new Intent(context,DoneActionReceiver.class);
        doneInt.putExtra("task", task);
        PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager notificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }
}

The manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="il.ac.she.dd.todoli"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".ListNodeActivity"
        android:label="@string/title_activity_list_node"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
    </activity>
    <activity
        android:name=".Signup_Activity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity
        android:name=".create_team"
        android:label="Create new task team"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".invite_member"
        android:label="Invite Members Your Team"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".Login_activity"
        android:label="Login to Wiggle"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EditTaskActivity"
        android:label="Edit Task"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

I have a feeling that I'm missing something very small but cannot find the issue. Please let me know what I'm doing wrong.

Thanks in advance.


Solution

  • you have to first build notification and then notify it...

    NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(this)
            .setContentTitle("New")
            .setContentText("hello")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pintent)
            .build();
    
    notification.vibrate=new long[]{100,250,100,500};
    _manager.notify(notification_id, notification);
    

    You can find a complete example of Alarm and Notification in my this GitHub Repo