在特定日期设置通知(片段)

时间:2017-01-15 03:57:29

标签: java android android-fragments notifications alarmmanager

我正在尝试在特定日期和时间弹出通知。变量很好,我已经测试过了。这是对我不起作用的代码。



Intent intent = new Intent(getActivity(), NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 001, intent, 0);

AlarmManager am = (AlarmManager)getActivity().getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, reminder.getTimeInMillis(), pendingIntent);




没有任何反应。完全没有。我无法弄清楚原因。这是我从谷歌上弹出的第一篇文章中得到的代码。我改变任何事情的唯一原因是因为我在一个片段内工作。



Calendar sevendayalarm = Calendar.getInstance();

sevendayalarm.add(Calendar.DATE, 7);

Intent intent = new Intent(this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 001, intent, 0);

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, sevendayalarm.getTimeInMillis(), pendingIntent);




我将以文本形式添加我的清单和NotificationPublisher.java文件。希望有人能弄明白问题是什么......



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<service android:name=".NotficationPublisher" android:enabled="true">
    <intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
</service>
&#13;
&#13;
&#13;

NOTIFICATIONPUBLISHER.JAVA

&#13;
&#13;
package com.vraces.vitte.senae;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import junit.framework.Test;

/**
 * Created by CmdrVraces on 2017-01-15.
 */

public class NotificationPublisher extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Intent intent = new Intent(this, Test.class);
        long[] pattern = {0, 300, 0};
        PendingIntent pi = PendingIntent.getActivity(this, 01234, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.warning)
                .setContentTitle("Take Questionnaire")
                .setContentText("Take questionnaire for Duke Mood Study.")
                .setVibrate(pattern)
                .setAutoCancel(true);

        mBuilder.setContentIntent(pi);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(01234, mBuilder.build());
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你需要改变这个:

PendingIntent.getBroadcast(getActivity(), 001, intent, 0);

PendingIntent.getService(getActivity(), 001, intent, 0);

因为NotificationPublisherService而不是BroadcastReceiver

答案 1 :(得分:0)

几周前我遇到过类似的问题,这个链接的答案帮助我解决了这个问题:Exact time for custom notifications in Android 当然你需要一些修改,但我希望它能以正确的方式向你展示......