0

我想为我的广告发出通知,所以我使用了 Alarmmanager 和 JobIntentService 但我没有收到通知。

这里是接收器。

    <receiver android:name="receiver.BootReceiver"
            android:enabled="true"
            android:exported="true">
        </receiver>
<service
            android:name=".services.JobService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false" />

以下是扩展 JobIntentService 的服务类。

    public class JobService extends JobIntentService {

    public static final int JOB_ID = 100;
    public static final int NOTIF_ID = 56;
    long timestamp;
    private NotificationManager mNotificationManager;
//    private final IBinder mBinder = new NotificationService.LocalBinder();

//    MediaPlayer mMediaPlayer = null;

    String MpStop;
    SharedPreferences mPreferences;
    float selectedLat, selectedLng;
    String name, userSect;
    boolean mAzanNotification;

    public static void enqueueWork(Context context, Intent work) {

        enqueueWork(context, JobService.class, JOB_ID, work);
    }
@Override
    protected void onHandleWork(@NonNull Intent intent) {
        mPreferences = Application.getAppContext().getSharedPreferences(Utils.PREF_NAME, Context.MODE_PRIVATE);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyTasker.run();
    }

在 notifyTasker isTimerTask() 中,我检查 currentTime 是否等于我的祈祷时间。如果是真的,那么我会触发通知。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
                    .setSmallIcon(R.mipmap.icon_beta)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                            R.mipmap.icon_beta))
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(text)
                    .setContentTitle(waktu)
                    .setAutoCancel(true) //                    .setSound(uri)
                    .setContentIntent(contentIntent);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                // Creating an Audio Attribute
               /* AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();*/

                // Creating Channel
                NotificationChannel notificationChannel = new NotificationChannel("CH_ID", "Testing_Audio", NotificationManager.IMPORTANCE_HIGH); //                notificationChannel.setSound(uri,audioAttributes);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
            Notification notification = notificationBuilder.build();
            notification.flags |=
                    Notification.FLAG_ONGOING_EVENT |
                            Notification.FLAG_SHOW_LIGHTS;
            notification.ledOnMS = 300;
            notification.ledOffMS = 3000;

            notification.flags |= Notification.FLAG_AUTO_CANCEL;

以下是我从活动中激活的警报管理器。

 Intent intent = new Intent(getMain(), BootReceiver.class);
        // Create a PendingIntent to be triggered when the alarm goes off
        alarmPendingIntent = PendingIntent.getBroadcast(getMain(), BootReceiver.REQUEST_CODE,
                intent, 0);
        // Setup periodic alarm every 5 seconds
        long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
        int intervalMillis = 60000; // as of API 19, alarm manager will be forced up to 60000 to save battery
        AlarmManager am = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
        // See https://developer.android.com/training/scheduling/alarms.html
        int minutes = 1;
        // by my own convention, minutes <= 0 means notifications are disabled
//        if (minutes > 0) {
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    SystemClock.elapsedRealtime() + minutes * 60 * 1000, minutes * 60 * 1000, alarmPendingIntent);

我尝试使用上述代码为 android oreo 触发通知,但它不起作用。是否有可能使用作业计划在特定时间触发通知。因为通知将在特定的 azan 时间触发。

4

1 回答 1

0

最好使用作业调度程序而不是警报管理器(请参阅此链接 - https://developer.android.com/reference/android/app/job/package-summary)。

“JobScheduler 保证可以完成您的工作,但由于它在系统级别运行,它还可以使用多种因素智能地安排您的后台工作与其他应用程序的工作一起运行。这意味着我们可以最大限度地减少无线电使用等事情, “这是一个明显的电池胜利。从 API 24 开始,JobScheduler 甚至考虑了内存压力,这对设备及其用户来说是一个明显的整体胜利。”

调度作业的示例:

JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(new JobInfo.Builder(LOAD_ARTWORK_JOB_ID, new ComponentName(this, DownloadArtworkJobService.class))
    .setBackoffCriteria(intervalsInMilliseconds, JobInfo.BACKOFF_POLICY_LINEAR);
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build());
于 2019-02-04T06:48:14.857 回答