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

标签 android firebase push-notification firebase-cloud-messaging google-cloud-functions

尽管 Cloud Functions 总是发送通知,但我的 Android 手机并不总是会收到通知。作为一个有用的侧节点:总是会收到 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/

相关文章:

android - 发生内部错误。 [MISSING_CLIENT_IDENTIFIER]

firebase - 类型 'List<dynamic>' 不是类型 'List<Widget>' 的子类型

push-notification - Electron APP的FCM/GCM

javascript - 通过一次通话向所有订阅者发送网络推送通知

Android fragment - 替换是添加一个 fragment

android - 为什么 Google map 在 Android 上不显示具体点?

javascript - 如何将服务注入(inject) AngularJS 的 UI-Router templateUrl 函数?

android - 当移动设备移动时,推送通知是否比套接字更好

android - windowSplashScreenAnimationDuration 有什么作用?

android - 将视频和图像从本地存储加载到 Google ChromeCast android