gpt4 book ai didi

java - 设置特定日期的通知( fragment )

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:42 27 4
gpt4 key购买 nike

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

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

什么也没发生。完全没有。我不明白为什么。这是我从谷歌上出现的第一篇文章中得到的代码。我更改任何内容的唯一原因是因为我正在 fragment 内工作。

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

我将以文本形式添加我的 list 和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>

通知发布者.JAVA

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

最佳答案

您需要更改此设置:

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

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

因为您的 NotificationPublisher 是一个 Service 而不是 BroadcastReceiver

关于java - 设置特定日期的通知( fragment ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41657676/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com