如何使用Phonegap Android Local Notification Plugin

时间:2013-10-04 19:56:04

标签: android cordova notifications phonegap-plugins localnotification

我正在为android使用phonegap locanotification插件。这是我第一次测试这个插件Local Notification

当我首次使用带有phonegap 2.7.0的插件时,它会给我带来很多错误。我终于通过谷歌搜索解决了所有错误。

当我运行此示例示例时,我根本没有收到任何通知

我已经按照config.xml中的read me文件和插件名称中的所有步骤进行了操作,但是当我运行此示例应用程序时,我没有收到任何错误和任何通知

有人可以告诉我哪里出错了吗

index.html:

<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="LocalNotification.js"></script>
<script type="text/javascript">

  document.addEventListener("deviceready", appReady, false);

  function appReady() {

    if (typeof plugins !== "undefined") {

      var now = new Date();
      now.setSeconds(now.getSeconds() + 10);
      console.log("Alarm Time Set " + now);

      plugins.localNotification.add({
        date : now,
        message : "Phonegap - Boooyyyaaaaah!\r\nUpyoass!",
        ticker : "Yeeeaaaaahhhh!!!",
        repeatDaily : false,
        id : 4
      });
    }
  };

</script>

Alarm Receiver.Java

package com.phonegap.plugin.localnotification;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.testing.R;

/**
 * The alarm receiver is triggered when a scheduled alarm is fired. This class
 * reads the information in the intent and displays this information in the
 * Android notification bar. The notification uses the default notification
 * sound and it vibrates the phone.
 * 
 * @author dvtoever
 */
public class AlarmReceiver extends BroadcastReceiver {

    public static final String TITLE = "ALARM_TITLE";
    public static final String SUBTITLE = "ALARM_SUBTITLE";
    public static final String TICKER_TEXT = "ALARM_TICKER";
    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

    /* Contains time in 24hour format 'HH:mm' e.g. '04:30' or '18:23' */
    public static final String HOUR_OF_DAY = "HOUR_OF_DAY";
    public static final String MINUTE = "MINUTES";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("AlarmReceiver", "AlarmReceiver invoked!");

        final Bundle bundle = intent.getExtras();
        final Object systemService = context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Retrieve notification details from the intent
        final String tickerText = bundle.getString(TICKER_TEXT);
        final String notificationTitle = bundle.getString(TITLE);
        final String notificationSubText = bundle.getString(SUBTITLE);
        int notificationId = 0;

        try {
            notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID));
        } catch (Exception e) {
            Log.d("AlarmReceiver", "Unable to process alarm with id: " + bundle.getString(NOTIFICATION_ID));
        }

        Calendar currentCal = Calendar.getInstance();
        int alarmHour = bundle.getInt(HOUR_OF_DAY);
        int alarmMin = bundle.getInt(MINUTE);
        int currentHour = currentCal.get(Calendar.HOUR_OF_DAY);
        int currentMin = currentCal.get(Calendar.MINUTE);

        if (currentHour != alarmHour && currentMin != alarmMin) {
            /*
             * If you set a repeating alarm at 11:00 in the morning and it
             * should trigger every morning at 08:00 o'clock, it will
             * immediately fire. E.g. Android tries to make up for the
             * 'forgotten' reminder for that day. Therefore we ignore the event
             * if Android tries to 'catch up'.
             */
            Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due");
            return;
        }

        // Construct the notification and notificationManager objects
        final NotificationManager notificationMgr = (NotificationManager) systemService;
        final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
                System.currentTimeMillis());
        final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.vibrate = new long[] { 0, 100, 200, 300 };
        notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);

        /*
         * If you want all reminders to stay in the notification bar, you should
         * generate a random ID. If you want do replace an existing
         * notification, make sure the ID below matches the ID that you store in
         * the alarm intent.
         */
        notificationMgr.notify(notificationId, notification);
    }
}

我的Android清单文件:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />


<application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity android:configChanges="orientation|keyboardHidden"
        android:name=".MyPhoneGapActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.phonegap.plugin.localnotification.AlarmReceiver" >
    </receiver>

    <receiver android:name="com.phonegap.plugin.localnotification.AlarmRestoreOnBoot" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

0 个答案:

没有答案