通知仅在App在后台运行时有效

时间:2019-07-29 08:56:29

标签: android android-notifications

我正在构建一个提醒应用程序。在那我添加了一个通知部分,它将通知用户有关已添加任务的信息。但是,仅当应用程序在后台运行时,通知才起作用。关闭时,通知不起作用。

我也想在应用程序关闭时得到通知。

ReminderAlarmService.java

import android.annotation.SuppressLint;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;

public class ReminderAlarmService extends IntentService {
    private static final String TAG = ReminderAlarmService.class.getSimpleName();

    private static final int NOTIFICATION_ID = 42;
    //This is a deep link intent, and needs the task stack
    public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
        Intent action = new Intent(context, ReminderAlarmService.class);
        action.setData(uri);
        return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public ReminderAlarmService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {


        Uri uri = intent.getData();
        Intent action = new Intent(this, AddReminderActivity.class);
        action.setData(uri);
        PendingIntent operation = TaskStackBuilder.create(this)
                .addNextIntentWithParentStack(action)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        //Grab the task description
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);

        String description = "";
        try {
            if (cursor != null && cursor.moveToFirst()) {
                description = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        Uri sounduri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            String id = "MY_CH_ID";
            CharSequence name = "Product";
            String desc = "Notifications regarding our products";
            int importance = NotificationManager.IMPORTANCE_MAX;
            @SuppressLint("WrongConstant")
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            mChannel.setDescription(desc);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.setSound(sounduri,attributes);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"MY_CH_ID");
                builder.setContentTitle(getString(R.string.reminder_title));
                builder.setContentText(description);
                builder.setSmallIcon(R.drawable.ic_add_alert_black_24dp);
                builder.setContentIntent(operation);
                builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
                builder.setSound(sounduri);
                builder.setAutoCancel(true);
                builder.setPriority(Notification.PRIORITY_MAX);
                builder.setChannelId("MY_CH_ID");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,builder.build());

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.remainder">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddReminderActivity"
            android:label="@string/title_activity_add_reminder"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme">
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"
                />
        </activity>
        <provider
            android:name=".AlarmReminderProvider"
            android:authorities="com.droidparkz.reminder"
            android:exported="false" />

        <service
            android:name=".ReminderAlarmService"
            android:exported="false" />

    </application>

</manifest>

我不知道为什么会这样。应用程序关闭时,通知正在我的moto e3电源设备上工作。并且在所有其他设备中都会失败。

0 个答案:

没有答案