我正在尝试通知作业服务运行的时间。它不起作用。我想知道在启用作业服务时提供通知的正确方法。请帮助我理解这些概念以及如何使其发挥作用? 注意:调度正在运行,我正在尝试添加Notification,但无效。
public class GuardianJobService extends JobService{
public final int REQUEST_CODE_ASK_PERMISSIONS = 1001;
@Override
public boolean onStartJob(JobParameters params) {
enableTracking();
addNotification();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.alert_icon)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
public void enableTracking() {
......
}
}
答案 0 :(得分:0)
我认为存在一些潜在的问题
答案 1 :(得分:0)
作业调度基本上是在后台运行长任务。您应该在 FirebaseMessagingService 中添加通知,然后在添加通知后启动作业以在后台执行任务。
答案 2 :(得分:0)
我遇到的问题是它有通知权限。
答案 3 :(得分:0)
是的,您只能进行本地通知(不是基于Firebase或基于服务器的通知),而且我了解如果您提出此问题,那么这将是您的唯一要求。
就我而言,我实现了计划用于检查事件的侦听器。如果满足我的要求,那么我只需调用以下给定的函数。
private void generateBigTextStyleNotification() {
String notificationChannelId =
NotificationUtil.createNotificationChannel(thisService);
String title = "Your title";
String msg = "Your message";
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
.bigText(msg)
.setBigContentTitle(title)
.setSummaryText("Your Summary");
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
thisService,
0,
new Intent(),
PendingIntent.FLAG_CANCEL_CURRENT
);
//Build and issue the notification.
// Notification Channel Id is ignored for Android pre O (26).
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(
thisService, notificationChannelId);
notificationCompatBuilder.setAutoCancel(true);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + thisService.getPackageName() + "YourSoundPath");
Notification notification = notificationCompatBuilder
// BIG_TEXT_STYLE sets title and content for API 16 (4.1 and after).
.setStyle(bigTextStyle)
// Title for API <16 (4.0 and below) devices.
.setContentTitle(title)
// Content for API <24 (7.0 and below) devices.
.setContentText(msg)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(alarmSound)
.setLargeIcon(BitmapFactory.decodeResource(
thisService.getResources(),
R.mipmap.ic_launcher))
.setContentIntent(notifyPendingIntent)
.setDefaults(NotificationCompat.FLAG_AUTO_CANCEL)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(thisService, R.color.secondary_background_color))
.setCategory(Notification.CATEGORY_EVENT)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(NotificationCompat.PRIORITY_MAX)
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(true)
// Adds additional actions specified above.
.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
答案 4 :(得分:0)
请创建一个通知频道,并在您的通知管理器中注册。构建通知时,请使用相同的通知渠道。如果您在Android版本大于或等于Oreo的设备上运行代码,则该应用不会触发通知,因此您需要具有Oreo及更高版本的通知通道。请参阅:https://developer.android.com/training/notify-user/build-notification