我正在 React Native 中开发视频通话应用程序,并且我正在使用Onesignal向其他用户发送带有数据的视频通话通知。我正在使用的机制:
User A: initiated call with User B, with notification data with **High Priority**
User B: Receives the notification, and it displayed as a normal notification.
我在 React Native 中使用NotificationServiceExtender来监听 Android 中的背景。现在我希望通知类似于:用户 B(接收者)的设备保持振动并且通知将持续到 30-40 秒,即使应用程序被杀死/后台或前台。我已经尝试使用callkeep但它不起作用,我希望通知的行为像Instagra/Whatsapp 任何帮助将不胜感激,这里是通知服务类:
package com.videocall;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.RemoteViews;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;
@SuppressWarnings("unused")
public class NotificationServiceExtension extends ReactContextBaseJavaModule implements OSRemoteNotificationReceivedHandler {
private final String CHANNEL_ID = "VideoCall";
private final String CHANNEL_NAME = "VideoCalling Channel";
@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
OSMutableNotification mutableNotification = notification.mutableCopy();
createChannel(context);
mutableNotification.setExtender(builder -> builder.setColor(context.getResources().getColor(R.color.colorPrimary)));
JSONObject data = notification.getAdditionalData();
Log.i("OneSignalExample", "Received Notification Data: " + data);
if(data.has("isCalling")) {
createChannel(context);
Uri notifySound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long[] vibratePattern = { 0, 100, 200, 300 };
mutableNotification.setExtender(builder -> builder.setOngoing(true));
mutableNotification.setExtender(builder -> builder.setOnlyAlertOnce(false));
mutableNotification.setExtender(builder -> builder.setAutoCancel(false));
mutableNotification.setExtender(builder -> builder.setChannelId(CHANNEL_ID));
mutableNotification.setExtender(builder -> builder.setCategory(NotificationCompat.CATEGORY_CALL));
mutableNotification.setExtender(builder -> builder.setSound(notifySound));
notificationReceivedEvent.complete(mutableNotification);
} else {
notificationReceivedEvent.complete(mutableNotification);
}
// If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
// To omit displaying a notification, pass `null` to complete()
}
public void createChannel(Context context) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Call Notifications");
channel.setSound(ringUri, audioAttributes);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Objects.requireNonNull(context.getSystemService(NotificationManager.class)).createNotificationChannel(channel);
}
}
@NonNull
@NotNull
@Override
public String getName() {
return null;
}
}