Swr7der Swr7der - 16 days ago 9
Android Question

local notification plugin not working phonegap

I am trying to develop an app that shows notification for particular time that is set by the user. I used the recommended 3rd party plugin for showing notification from katzer local notification plugin.

I used all the things as described in the documentation of it.I have used function createTask() to initiating the notification for a particular time. I used the example time "10/27/2016 10:55:00" for just checking the notification for example time. As shown in the script part, I used alert to see if the time is shown in standard manner ,and I got the answer in "yes", the alert for it is showing.

The main problem is after that I wanted to see if the permission is given to the app or not and for that I checked with

cordova.plugins.notification.local.hasPermission(function(granted)

and I used alert("True/False "+granted) for showing alert if the function is called. But after the time alert there is nothing showing in it. Even the function is not being called or else the alert would be shown as described.

My codes are:

tasks.html:



<!doctype html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/materialize.css" media="screen,projection">
<link rel="stylesheet" type="text/css" href="css/custom-style.css" media="screen,projection">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Untitled Document</title>
</head>

<body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="js/cordova.js"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.js"></script>

<div class="taab-title">Task Manager</div>

<div class="row">
<div class="col s12">
<div class="task-creation">
<label for="add_task_title">Task Title</label>
<input placeholder="Write Your Task Here" id="add_task_title" type="text" class="validate">

<input type="date" class="datepicker" id="date_add_task">

<div class="row">
<div class="col s4">
<label for="hour_add_task">Hour</label>
<input placeholder="Hours" id="hour_add_task" type="text" >
</div>
<div class="col s4">
<label for="minutes_add_task">Mintues</label>
<input placeholder="Minutes" id="minutes_add_task" type="text" class="validate">
</div>
<div class="col s4 date-p">
<select id="ampm_add_task">
<option value="1" selected>AM</option>
<option value="2">PM</option>
</select>

</div>
</div>
<a class="waves-effect waves-light btn blue darken-3" style="width:100%;" onclick="createTask()">Create Task</a>

</div>

<script type="text/javascript" > **Line script**
var info = null;

document.addEventListener("deviceready", function(){
if(!localStorage.getItem("rp_data"))
{
var rp_data = {data: []};
localStorage.setItem("rp_data", JSON.stringify(rp_data));
}

info = JSON.parse(localStorage.getItem("rp_data"));
}, false);

function schedule(id, title, message, schedule_time)
{
alert("set");
cordova.plugins.notification.local.schedule({
id: id,
title: title,
message: message,
at: schedule_time
});

var array = [id, title, message, schedule_time];
info.data[info.data.length] = array;
localStorage.setItem("rp_data", JSON.stringify(info));

navigator.notification.alert("Reminder added successfully")
}

function createTask()
{


var schedule_time = new Date(("10/27/2016 10:55:00").replace(/-/g, "/")).getTime();
schedule_time = new Date(schedule_time);
alert(schedule_time);

var id = info.data.length;

cordova.plugins.notification.local.hasPermission(function(granted){
alert("True/False "+granted);
if(granted == true)
{
schedule(id, "1234", "msg", schedule_time);
}
else
{
cordova.plugins.notification.local.registerPermission(function(granted) {
if(granted == true)
{
schedule(id, "1234", "msg", schedule_time);
}
else
{
navigator.notification.alert("Reminder cannot be added because app doesn't have permission");
}
});
}
});
}

</script>
</body>
</html>





config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.reminder.app" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<preference name="loglevel" value="DEBUG" />
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
<feature name="Device">
<param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="LocalNotification">
<param name="android-package" value="de.appplant.cordova.plugin.localnotification.LocalNotification" />
</feature>
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<allow-intent href="market:*" />
<name>ReminderApp</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />




Manifest.xml:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.reminder.app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.TriggerReceiver" />
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.ClearReceiver" />
<activity android:exported="false" android:launchMode="singleInstance" android:name="de.appplant.cordova.plugin.localnotification.ClickActivity" android:theme="@android:style/Theme.NoDisplay" />
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.notification.TriggerReceiver" />
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.notification.ClearReceiver" />
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.RestoreReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:exported="false" android:launchMode="singleInstance" android:name="de.appplant.cordova.plugin.notification.ClickActivity" android:theme="@android:style/Theme.NoDisplay" />
</application>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />




I also tried the codes from Here , and when I tried to run the apk of the codes described in th link, all the things are correctly working and notification shows where ever it should show.

But same thing is not working in my app.

I don't know what is wrong with this.I searched nearly every link on Stackoverflow, but nowhere my problem is described. I am a beginner in App development from phonegap.

Someone please help me in determining the problem and solving it.

Answer

This plugin works well!

You should try debugging your app.You can do that in chrome://inspect or you can try this link,and choose any debugging type which fits suitable to your purpose.It will help you in getting the error if any will be present in the app.