簡體   English   中英

Android 通知未按預期工作

[英]Android notification not working as expected

我想創建每 3 天出現一次的帶有聲音和振動的通知。 我不能讓它發生

幾乎嘗試了我在互聯網上找到的所有解決方案。 你會從我的代碼中看到它。 使用我的代碼,聲音不起作用,振動不起作用,即使取消通知也會連續顯示更多次,即使我將優先級設置為高,它也不會在鎖定時顯示在屏幕上。 超級奇怪

這是在 MainActivity 中設置通知的函數:

public void setNotification() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 45);

        Intent myIntent = new Intent(this, NotifyService.class);
        int ALARM1_ID = 10000;
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this, ALARM1_ID, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 3, pendingIntent);
    }

這是觸發通知的 BroadcastReceiver:

public class NotifyService extends BroadcastReceiver {

    @SuppressLint("ResourceAsColor")
    @Override
    public void onReceive(Context context, Intent intent) {

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("my_channel_01ee",
                        "Channel human readable titlee",
                        NotificationManager.IMPORTANCE_HIGH);
                channel.enableVibration(true);
                channel.setVibrationPattern(new long[]{
                        0
                });

                channel.enableLights(true);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

                channel.setLightColor(Color.GRAY);
                channel.enableLights(true);
                channel.setDescription("descrioptiom");
                    AudioAttributes audioAttributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .build();
                channel.setSound(alarmSound, audioAttributes);
                notificationManager.createNotificationChannel(channel);

            }

            Intent notificationIntent = new Intent(context, FoodInspectorActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notification = new NotificationCompat.Builder(context, "my_channel_01ee");

            int color = 0xfffffff;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notification.setSmallIcon(R.drawable.magnifier_final);
                notification.setColor(color);
            } else {
                notification.setSmallIcon(R.drawable.magnifier_final);
            }

            notification.setContentTitle("FoodScan")
                    .setContentText("Scan some new products? Just click!")
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.launcher_icon))
                    .setSound(alarmSound)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_LIGHTS )
                    .setVibrate(new long[]{0, 500, 1000});

            assert notificationManager != null;
            notificationManager.notify(5, notification.build());

        }
}

使用此代碼,聲音不起作用,振動不起作用,即使取消通知也會連續顯示更多次,即使我將優先級設置為高,它也不會在鎖定時顯示在屏幕上。 超級奇怪

第一步:在你的MainActivity創建Method並使用AlarmManager設置指定時間的鬧鍾,並在OnCreate調用該方法

public void my(){

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,21);
calendar.set(Calendar.MINUTE,47);
if (calendar.getTime().compareTo(new Date()) < 0) 
calendar.add(Calendar.DAY_OF_MONTH, 1);
Intent intent = new Intent(getApplicationContext(),NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if (alarmManager != null) {
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
}


 }

我每天晚上09:47設置鬧鍾

第 2 步:創建 BroadcastReceiver 以在警報發生時進行偵聽

public class NotificationReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {

   NotificationHelper notificationHelper = new NotificationHelper(context);
   notificationHelper.createNotification();

   }
}

我正在創建這個名為NotificationReceiver類並擴展BroadcastReceiver ,在onReceive有一個名為NotificationHelper類,不要混淆,我將在下一步中解釋這個類。

第 3 步:創建 Notification 類

class NotificationHelper {

 private Context mContext;
 private static final String NOTIFICATION_CHANNEL_ID = "10001";

 NotificationHelper(Context context) {
 mContext = context;
 }

void createNotification()
{

Intent intent = new Intent(mContext , NotificationActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
        0 /* Request code */, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Title")
        .setContentText("Content")
        .setAutoCancel(false)
        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
        .setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.enableVibration(true);
    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    assert mNotificationManager != null;
    mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
    mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

   }
}

這個類處理通知

第 4 步:回到第 2 步:並調用通知類

NotificationHelper notificationHelper = new NotificationHelper(context);
notificationHelper.createNotification();

最后去你的AndroidManifest.xml並注冊這個

<receiver android:name=".NotificationReceiver"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM