gpt4 book ai didi

android - 并非总是通过电话收到通知?

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:30 25 4
gpt4 key购买 nike

我的 Android 手机并不总是收到通知,即使 Cloud Functions 总是 发送通知。作为一个有用的侧节点:总是收到 WhatsApp 消息(我测试过)。

我注意到当手机正在充电或打开应用程序时会立即收到通知。

此外,即使显示了通知,用户的 UID 也并不总是发送到数据库(您将通过阅读代码理解我的意思)。

我的问题是:我可以更改什么以接收通知,然后像预期的那样一直推送到数据库?

我的通知是“提醒”类型的。

几天,我一直在尝试优化我的代码,但没有成功。我需要帮助。

代码结构:

  1. 收到消息后,将用户的UID推送到数据库,看看有多少人收到通知。
  2. 向用户发送通知。

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    // Firebase instance variables
    private FirebaseAuth mFirebaseAuth;
    private FirebaseUser mFirebaseUser;

    DatabaseReference myURL = FirebaseDatabase.getInstance().getReferenceFromUrl("https://newproj-54a87.firebaseio.com/notified");

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

    // Check if message contains a data payload.
    if (remoteMessage.getData() != null) {

    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();

    if(mFirebaseUser != null)
    mUID = mFirebaseUser.getUid();

    myURL.push().setValue(mUID);

    sendNotification(remoteMessage.getData().get("body"), remoteMessage.getData().get("title"), remoteMessage.getData().get("icon"));
    }
    }

    private void sendNotification(String messageBody, String messageTitle, String iconURL) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    PendingIntent.FLAG_ONE_SHOT);
    Bitmap bitmap = getBitmapFromURL(iconURL);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
    .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
    .setPriority(Notification.PRIORITY_MAX)
    .setStyle(new NotificationCompat.BigTextStyle())
    .setSmallIcon(R.drawable.myIcon)
    .setLargeIcon(bitmap)
    .setContentTitle(messageTitle)
    .setContentText(messageBody)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

    public Bitmap getBitmapFromURL(String strURL) { //from https://stackoverflow.com/a/16007659
    try {
    URL url = new URL(strURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
    } catch (IOException e) {
    e.printStackTrace();
    return null;
    }
    }
    }

我的云函数:

    const payLoad = {
data:{
title: name,
body: body,
icon: photoURL
}
};

var options = {
priority: "high",
timeToLive: 1000
};

return admin.messaging().sendToTopic("notify", payLoad, options);

如果我忘记了某条信息,请告诉我。我正在限时运行!

最佳答案

如果您使用的是 Android 6.0 或更高版本,则有一个名为 Doze 的功能可以优化电池使用。当您的手机处于空闲状态(并且未充电)时,它只会收到高优先级的 FCM 通知,您的情况可能就是这样。

Android documentation说:

FCM is optimized to work with Doze and App Standby idle modes by means of high-priority FCM messages. FCM high-priority messages let you reliably wake your app to access the network, even if the user’s device is in Doze or the app is in App Standby mode. In Doze or App Standby mode, the system delivers the message and gives the app temporary access to network services and partial wakelocks, then returns the device or app to the idle state.

因此,如果使用 FCM 数据消息,您的后端应该开始发送字段 "priority": "high"
引用:https://firebase.google.com/docs/cloud-messaging/concept-options

关于android - 并非总是通过电话收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com