gpt4 book ai didi

java - 如果我不访问该应用程序,则不会收到通知

转载 作者:行者123 更新时间:2023-12-01 16:14:48 25 4
gpt4 key购买 nike

大家好,我遇到了这个问题,我编写了一个应用程序,通过 Firebase Cloud Messaging 在代码下方发送通知:

package com.app.name.Service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.fourapper.forpaper.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;
import java.util.Random;

public class MyFirebaseService extends FirebaseMessagingService {

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

if(remoteMessage.getData().isEmpty()){

showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());

}else{

showNotification1(remoteMessage.getData());
}

}

private void showNotification1(Map<String, String> data) {

String title = data.get("title").toString();
String body = data.get("body").toString();


NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.app.name.Test";

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification", NotificationManager.IMPORTANCE_HIGH);

notificationChannel.setDescription("Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 0, 1000, 500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_money_bag)
.setVibrate(new long[]{500, 500})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");

notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}

private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.app.name.Test";

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification", NotificationManager.IMPORTANCE_HIGH);

notificationChannel.setDescription("Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 0, 1000, 500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_money_bag)
.setVibrate(new long[]{500, 500})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");

notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}

@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);

Log.d("TOKENFIREBASE", s);
}
}

一切正常,只是我遇到了这个问题,因为我已经安排了发送每日通知,我注意到,如果我在当天不访问该应用程序,则通知不会发送给我,相反,如果我访问该应用程序在收到通知的当天,我不明白问题出在哪里,如果您可以帮助我,谢谢

最佳答案

下面是我的 list 代码,您可以看到我将所有内容都放在 Service 文件夹中的 Service 类中:

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

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<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"
android:usesCleartextTraffic="true"
tools:targetApi="m">

<!-- AdMob App ID: -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value=""/>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />

<activity android:name=".MainActivity">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service
android:name=".Service.MyFirebaseService"
tools:ignore="ExportedService,InnerclassSeparator">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

</application>

</manifest>

关于java - 如果我不访问该应用程序,则不会收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62431874/

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