【问题标题】:Can I send Notification using job scheduler?我可以使用作业调度程序发送通知吗?
【发布时间】:2018-05-02 15:09:17
【问题描述】:

我正在尝试在作业服务运行时发出通知。它不工作。我想知道启用 Job 服务时提供通知的正确方法。请帮助我理解这些概念以及如何使其发挥作用? 注意:计划正在运行,我正在尝试添加通知,但它不起作用。

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() {  
    ......
}

}

【问题讨论】:

    标签: android android-notifications jobservice


    【解决方案1】:

    作业调度基本上是在后台运行长任务。您应该在 FirebaseMessagingService 中添加通知,然后在添加通知后启动作业以在后台执行任务。

    【讨论】:

      【解决方案2】:

      是的,我已经实现了监听器,它计划检查事件。如果这符合我的要求,那么我只需调用下面给出的函数。

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

      Bahman 感谢提问,这是我们的 NotificationUtil 类:

      public class NotificationUtil {
      public static String createNotificationChannel(
              Context context) {
      
          // NotificationChannels are required for Notifications on O (API 26) and above.
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      
              // The id of the channel.
              String channelId = "YOUR_CHANNEL_ID";
      
              // The user-visible name of the channel.
              CharSequence channelName = "YOUR_CHANNEL_NAME";
              // The user-visible description of the channel.
              String channelDescription = "YOUR_CHANNEL_DESCRIPTION";
              int channelImportance = NotificationManager.IMPORTANCE_HIGH;
              int channelLockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC;
      
              // Initializes NotificationChannel.
              NotificationChannel notificationChannel = null;
              notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
      
              notificationChannel.setDescription(channelDescription);
              notificationChannel.enableVibration(true);
              notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
      
              // Adds NotificationChannel to system. Attempting to create an existing notification
              // channel with its original values performs no operation, so it's safe to perform the
              // below sequence.
              NotificationManager notificationManager =
                      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
              notificationManager.createNotificationChannel(notificationChannel);
      
              return channelId;
          } else {
              // Returns null for pre-O (26) devices.
              return null;
          }
      }
      

      }

      【讨论】:

      • 什么是 NotificationUtil? Android Studio 正在抱怨它。
      • @Bahman 我也添加了 NotificationUtil 类。快乐编码!!!!
      【解决方案3】:

      我认为有一些潜在的问题

      • 忘记在 AndroidManifest.xml 中添加 GuardianJobService
      • 从未满足运行作业的条件(您能出示您的日程安排代码吗?)
      • 如果您在 Android >= 8.0 的设备上运行,则需要创建通知通道以接收通知(请参阅 here

      【讨论】:

      • 调度工作正常,但通知不起作用
      • 我尝试了您的通知代码,它在我的设备(Android 6)上运行良好。如果您使用的是 Android >= 8.0.您应该创建通知渠道。否则,您能否尝试使用一些随机通知 id 而不是零“manager.notify(random_notification_id_here, builder.build());”
      【解决方案4】:

      请创建一个通知渠道并将其注册到您的通知管理器。构建通知时使用相同的通知渠道。如果您在 Android 版本大于或等于 Oreo 的设备上运行代码,则该应用不会触发通知,因此您需要为 Oreo 及更高版本提供通知通道。参考:https://developer.android.com/training/notify-user/build-notification

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-10
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多