【问题标题】:Android Notification Not Appearing As Heads UpAndroid 通知未显示为抬头
【发布时间】:2018-11-08 10:45:27
【问题描述】:

我正在使用以下内容向 Android 上的用户显示当前工作正常的通知,但我遇到了一个问题,即通知显示在状态栏中,但不像 Facebook 或 WhatsApp 通知那样作为提示出现设备?我收到通知,但必须下拉状态栏才能查看。我想知道有没有办法让它以气泡格式显示在屏幕顶部,或者这是否因手机设置而异?

代码附在下面:

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addNotification(10,"eventname","roomname");
        addNotification(25,"eventname2","roomname2");
    }
    public void addNotification(int test, String test2, String test3){
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("test",test2);
        intent.putExtra("test2",test3);
        final int _id = 50;
        Random random = new Random();
        final int randomInt = random.nextInt();
        System.out.println("random integer:" + randomInt);
        PendingIntent appIntent = PendingIntent.getBroadcast(this, randomInt, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, test);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), appIntent);
    }
}

报警接收器

public class AlarmReceiver extends BroadcastReceiver{
    private static final String CHANNEL_ID = "com.singhajit.notificationDemo.channelId";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        String passed = intent.getStringExtra("test");
        String passed2 = intent.getStringExtra("test2");
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(NotificationActivity.class);
        stackBuilder.addNextIntent(notificationIntent);
String messageBody = "Your event " + passed + " is about to start in 15 minutes, in room "+passed2;
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(context);

        builder.setStyle(new Notification.BigTextStyle(builder)
                .bigText(messageBody)
                .setBigContentTitle("UA Reloaded Event Starting")
                .setSummaryText("Tap To View Info"))
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_MAX);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

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

            String name = "NotificationDemo";
            String description = "NotificationDemo";
            int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
            NotificationChannel channel = new NotificationChannel("1", name, importance);
            channel.setDescription(description);
            channel.setShowBadge(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
           // NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0, builder.build());
    }
}

【问题讨论】:

  • 为什么还要显示为气泡?您只是在为 android 创建一个正常的通知。气泡只是应用细节
  • 我希望它显示在顶部并且能够阅读应用程序通知的内容而无需下拉状态栏,现在通知使手机嗡嗡声并出现应用程序徽标状态栏,但在我下拉状态栏@finki之前不会显示我的通知
  • 是的。这正是您的代码应该做的。好像您是 android 新手,请查看有关您请求的功能的视频:youtube.com/watch?v=LoO0nLpFfWU
  • @finki 我认为你误会了。我不想要像 facebook 那样的泡沫。我希望它是一个提醒通知,例如:developer.android.com/guide/topics/ui/notifiers/notifications
  • 然后更新您的问题并与抬头交换气泡!我会尝试将通知的重要性设置为高/紧急。正如文档指定的那样:developer.android.com/guide/topics/ui/notifiers/…

标签: android alarmmanager android-notifications android-alarms notificationmanager


【解决方案1】:

您更新了您的频道重要性,如文档 (https://developer.android.com/training/notify-user/channels#CreateChannel) 中所述,这是不可能的。

因此您的问题应该通过将channelId 更改为"1" 以外的其他值来解决,因为频道的ids 必须是唯一的。

【讨论】:

    【解决方案2】:

    如果您仍需要答案或其他任何人以便将通知显示为提示,您必须将您的频道 ID 添加到 Builder。 .setChannelId(CHANNEL_ID)

    像这样:

      val notification = NotificationCompat.Builder(getContext(), CHANNEL_ID)
                .setSmallIcon(...)
                .setContentTitle(getContext().getString(R.string.app_name))
                ...
                .setChannelId(CHANNEL_ID)
                ...
                .build()
    

    不要忘记 NotificationChannel 的重要性和通知优先级(如果需要,将它们设置为高/最大值)

    【讨论】:

      【解决方案3】:

      这是我的 kotlin 类,你只需要调用 notify(title: String, text: String) 方法,如果你想在 java 中使用它,你可以转换它

      import android.annotation.SuppressLint
      import android.app.Notification
      import android.app.NotificationManager
      import android.content.Context
      import android.support.v4.app.NotificationCompat
      import beacon.geisoft.org.beacontrakerkotlin_rebuild.R
      import android.os.Build
      import android.support.annotation.RequiresApi
      import android.support.v4.content.ContextCompat.getSystemService
      import android.app.NotificationChannel
      import android.app.PendingIntent
      import android.content.Intent
      import android.graphics.Color
      import android.media.RingtoneManager
      import android.support.v4.content.ContextCompat.getSystemService
      import android.support.v4.app.NotificationManagerCompat
      import beacon.geisoft.org.beacontrakerkotlin_rebuild.activities.MainActivity
      import android.preference.PreferenceManager
      import android.content.SharedPreferences
      
      
      
      
      class Notifications (var context: Context){
      
      /**
       * Send notification to the client device
       * @param text String
       */
      @SuppressLint("PrivateResource")
      private fun notificate(title: String, text: String, id: Int, notificationManager: NotificationManager) {
      
          val intent1 = Intent(context, MainActivity::class.java)
          val pendingIntent = PendingIntent.getActivity(context, 123, intent1, PendingIntent.FLAG_UPDATE_CURRENT)
          val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel("beacon.geisoft.org.beacontraker_rebuild") == null) {
      
              val chan2 = NotificationChannel("beacon.geisoft.org.beacontraker_rebuild", "Pazienti", NotificationManager.IMPORTANCE_HIGH)
              chan2.lightColor = Color.BLUE
              chan2.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
              notificationManager.createNotificationChannel(chan2)
      
              /*
              notificationManager.createNotificationChannel(NotificationChannel("beacon.geisoft.org.beacontraker_rebuild",
                      "Pazienti", NotificationManager.IMPORTANCE_HIGH))*/
          }
          val builder = NotificationCompat.Builder(context, "beacon.geisoft.org.beacontraker_rebuild")
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              builder.setContentTitle(title)  // required
                      .setContentText(text)  // required
                      .setDefaults(Notification.DEFAULT_ALL)
                      .setAutoCancel(true)
                      .setContentIntent(pendingIntent)
                      .setSmallIcon(R.drawable.beaconicon32) // required
                      .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.beaconicon64))
                      .setSound(defaultSoundUri)
          }else {
              builder.setAutoCancel(true)
                      .setDefaults(Notification.DEFAULT_ALL)
                      .setContentTitle(title)
                      .setContentText(text)
                      .setContentIntent(pendingIntent)
                      .setSmallIcon(R.drawable.beaconicon32) // required
                      .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.beaconicon64))
                      .setPriority(NotificationCompat.PRIORITY_HIGH)
                      .setSound(defaultSoundUri)
          }
          notificationManager.notify(id, builder.build());
      }
      
      fun notificate(title: String, text: String, id: Int){
          val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
          notificate(title, text, id, notificationManager!!)
      }
      
      fun notificate(title: String, text: String){
          val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
          var num: Int
          do {
              num = (Math.random() * 100).toInt()
          } while (notificationExist(notificationManager!!, num))
          notificate(title, text, num, notificationManager)
      }
      
      fun notificationExist(notificationManager: NotificationManager, id: Int): Boolean {
      
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              val notifications =
                      notificationManager.activeNotifications
              for (notification in notifications) {
                  if (notification.getId() == id) {
                      return true
                  }
              }
          }
          return false
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-04
        • 1970-01-01
        • 2021-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多