自定义 notification/Vibration - Firebase - 磨损 OS - Android 8.0.0。 API 26

Custom notification/Vibration - Firebase - wear OS - Android 8.0.0. API 26

我拥有的:Wear OS(Android 8+)、API 26 和 Firebase 中的独立应用程序。

我要实现的目标(第一):我会显示带有自定义振动模式的自定义通知。

我会实现什么(第二):当我的应用程序在后台时,我会自动打开我的应用程序或点击通知(但有自定义振动)

我是运行前台服务,有一个持续的通知。正在进行的通知工作得很好,并且没有 Wear OS 的功能(因此代码可以在独立 Android 上工作)。我有一些关于通知的问题。

我无法更改振动通知和布局。

这里是我的 FirebaseMessagingService

public class MyfirebaseMessagingService extends FirebaseMessagingService {

    public static String TAG = "MyFirebaseMsgService";

    private LocalBroadcastManager broadcaster;

    Uri NOTIFICATION_SOUND_URI = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" );
    long[] VIBRATE_PATTERN    = {0, 500};

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();

        broadcaster = LocalBroadcastManager.getInstance(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = getString(R.string.default_notification_channel_id);

            CharSequence name = getString(R.string.default_notification_channel_id);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Intent home = new Intent(getApplicationContext(), Splash.class);
        home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(home);

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

            Intent intent = new Intent("Data");
            intent.putExtra("messageFromCloud", remoteMessage.getNotification().getBody());
            broadcaster.sendBroadcast(intent);
            if (intent.getExtras() != null)
            {
                MyNotificationManager notification =  MyNotificationManager.getInstance(this);
                notification.displayNotification("title",remoteMessage.getNotification().getBody());

                RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");

                for (String key : intent.getExtras().keySet())
                {
                    builder.addData(key, Objects.requireNonNull(intent.getExtras().get(key)).toString());
                }
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationManager mNotificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    int importance = NotificationManager.IMPORTANCE_HIGH;
                    NotificationChannel mChannel = new NotificationChannel(Constants.CHANNEL_ID, Constants.CHANNEL_NAME, importance);
                    mChannel.setDescription(Constants.CHANNEL_DESCRIPTION);
                    mChannel.enableLights(true);
                    mChannel.setLightColor(Color.RED);
                    mChannel.enableVibration(true);
      //                    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    mNotificationManager.createNotificationChannel(mChannel);

                     Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                    long[] newvibrationPattern = {0, 1000, 500, 50,
                                0, 1000, 500, 50,
                                0, 1000, 500, 50,
                                0, 1000, 500, 50,
                                0, 1000, 500, 50,
                                0, 1000, 500, 50};
                     //-1 - don't repeat
                     final int indexInPatternToRepeat = -1;
                     vibrator.vibrate(newvibrationPattern, indexInPatternToRepeat);
                }
                onMessageReceived(builder.build());
            }
        }
    }
}

我终于设法避开了自定义振动模式。你的代码很乱 DoctorWho。 像你一样,我也尝试了 NotificationCompat.BuilderNotificationChannel 的各种组合,但没有任何效果。为了让自定义振动正常工作,您有两个选择,不显示通知,只播放您的自定义振动,或者您可以显示通知,但您必须实施它,以便为此禁用振动 NotificationChannel您构建通知并在播放自定义振动后延迟 1 秒。 示例:

val notificationBuilder = NotificationCompat.Builder(this, "channelId")
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val chan = NotificationChannel(
                "channelId",
                "Default",
                NotificationManager.IMPORTANCE_HIGH
            )
            chan.enableVibration(false)
            notificationManager.createNotificationChannel(chan)
        }

现在您可以 运行 延迟 1 秒后的振动模式:

Timer().schedule(1000) {
        vibrate(pattern)
}
private fun vibrate(pattern: LongArray) {
        val v = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

        if (v.hasVibrator()) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createWaveform(pattern, -1))
            } else {
                //deprecated in API 26
                @Suppress("DEPRECATION")
                v.vibrate(pattern, -1)
            }
        }
    }

并且不要忘记在清单中设置权限:

<uses-permission android:name="android.permission.VIBRATE" />

除振动功能外的所有这些都会进入您的 override fun onMessageReceived(msg: RemoteMessage) 功能。