Android通知不显示为抬头

时间:2018-11-08 10:45:27

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

我正在使用以下内容向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);
    }
}

AlarmReceiver

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

2 个答案:

答案 0 :(得分:0)

您更新了频道的重要性,而此重要性无法按照文档(https://developer.android.com/training/notify-user/channels#CreateChannel)中的说明进行。

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

答案 1 :(得分:0)

这是我的kotlin类,您所需要做的就是调用notificate(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
}
}