Android - 警报通知问题

时间:2015-12-25 20:07:02

标签: android android-manifest android-notifications android-alarms

我尝试在Android中配置警报通知,但遗憾的是没有生成警报。

这是警报的创建:

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);

此外我还创建了: 的 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);
    }
}

清单

<?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>

我有一种感觉,我错过了一些非常小但却找不到问题的东西。 请让我知道我做错了什么。

提前致谢。

1 个答案:

答案 0 :(得分:1)

你必须先建立通知,然后通知它......

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);

您可以在我的GitHub Repo

中找到警报和通知的完整示例