我正在使用Firebase,并在应用程序处于后台时测试从我的服务器向我的应用发送通知.通知成功发送,甚至出现在设备的通知中心,但是当通知出现或甚至我点击它时,我的FCMessagingService内的onMessageReceived方法永远不会被调用.
当我在我的应用程序位于前台时对此进行测试时,调用了onMessageReceived方法,一切正常.当应用程序在后台运行时会出现此问题.
这是预期的行为,还是有办法解决这个问题?
这是我的FBMessagingService:
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FBMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i("PVL", "MESSAGE RECEIVED!!");
if (remoteMessage.getNotification().getBody() != null) {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
} else {
Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在SO上读过类似的问题,然而,我无法从中得到正确的答案.
我有一个系统,我们向大约500个设备发送通知.
不幸的是,许多这些设备没有收到通知.我发现OPPO F1系列手机特别没有收到通知.
我发现,如果应用程序从多任务托盘中停止,则会发生这种情况.我该如何解决这个问题?
更新:我观察到当我从任务托盘关闭应用程序时,我的应用程序被强制停在应用程序管理器中.当我从任务托盘关闭Whatsapp时,仍然没有强制停止.怎么被Whatsapp处理?
android firebase firebase-cloud-messaging firebase-notifications
当用户在后台点击通知时,我正在尝试打开特定活动.从Docs中,我已经知道必须在有效负载中添加click_action,并在App中使用intent过滤来处理它.但是,如何通过Firebase控制台在Firebase通知中添加click_action?我也对任何其他工作开放.提前致谢.
android android-notifications firebase-cloud-messaging firebase-notifications
应明确实施如何使用Firebase通知和数据.我读了很多答案,但似乎无法使其发挥作用.这是我的步骤:
1.)我在PHP中将通知和数据传递给android,它看起来很好:
$msg = array
(
"body" => $body,
"title" => $title,
"sound" => "mySound"
);
$data = array
(
"user_id" => $res_id,
"date" => $date,
"hal_id" => $hal_id,
"M_view" => $M_view
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg,
'data' => $data
);
$headers = array
(
'Authorization: key='.API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( …Run Code Online (Sandbox Code Playgroud) notifications android firebase firebase-realtime-database firebase-cloud-messaging
我希望在收到通知时自动打开应用程序,Firebase和新的FCM通知是否可以实现?
我知道我可以设置click_action,但这只是为了自定义通知点击会启动哪些活动,我需要在收到通知时自动启动的内容.
我尝试了快速启动消息传递firebase示例,并且有一个onMessageReceived()方法,但只有在应用程序位于前台时它才有效.应用程序在后台运行时是否会执行某些操作?GCM可以通过直接启动广播接收器的活动意图来做我喜欢的事情,广播接收器在收到通知时调用.
android firebase firebase-cloud-messaging firebase-notifications
//身体就像这样
{
"to":
"/topics/NEWS"
,
"data":{
"extra_information": "This is some extra information"
},
Run Code Online (Sandbox Code Playgroud)
//我需要提供的通知
"notification":{
"title": "ChitChat Group",
"text": "You may have new messages",
"click_action":"ChatActivity"
}
}
Run Code Online (Sandbox Code Playgroud) 如图所示......
我收到了通知图标(左侧是红色).
但我需要显示黑色箭头所示的应用程序图标
public void notify(View view){
notification.setSmallIcon(R.drawable.ic_stat_name);
notification.setTicker("Welcome to ****");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("abcd");
notification.setContentText("abcd");
Intent intent = new Intent(this, home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
Run Code Online (Sandbox Code Playgroud) notifications android android-notifications android-notification-bar android-studio
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
mBuilder.setSound(sound);
Run Code Online (Sandbox Code Playgroud)
我已将mp3(notification_mp3.mp3)文件复制到res文件夹中的raw文件夹中.当通知被触发时,它产生给定的mp3声音到Android Nougat但在Android Oreo中默认声音.我曾推荐过许多网站,但在Android Oreo上没有任何效果.我没有发现Android Docs中有关Android O及以上版本的通知声音的任何更改.应该做些什么更改才能使此代码在Android O中运行?
只有当应用程序不在前台时,我才需要向用户显示通知.这是我的公共类MyFirebaseMessagingService扩展
FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(applicationInForeground()) {
Map<String, String> data = remoteMessage.getData();
sendNotification(data.get("title"), data.get("detail"));
}
}
Run Code Online (Sandbox Code Playgroud)
需要实施applicationInForeground()方法
我想在我的应用中制作一个简单的通知功能.我按照此 YouTube视频和这两个Firebase文档网址1和2以及Android Studio中的Firebase工具助手(说我已连接到Firebase).出于某种原因,在我的旧应用程序(下面发布的代码)上执行这些步骤和文档后,它将不允许我接收通知.但是,如果我在一个全新的应用程序上执行相同的步骤,它将完美地运行.我在后台,活动和终止状态下测试了同一物理设备和环境中的两个应用程序.每次我创建的新演示应用程序都没有问题,但我想要通知的旧应用程序不起作用.两者均未经过设备ID测试.我甚至没有得到任何错误日志或任何TAG日志.我认为我的一个编译项目是干扰的,不确定究竟是什么,但我可能要看那里.
PS.我在下面删除了我的包名,我在FireBase上多次检查并且它们匹配,所以我知道这不是问题.但是,我的新演示应用程序的FireBase显示我的应用程序连接但我的旧应用程序的FireBase没有.此外,我之前已经设置了赏金,但仍然遇到同样的问题.
这是我有问题的代码:
Notification.java(它的文档要求的服务)
public class Notification extends FirebaseMessagingService {
public Notification() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
Log.d("FMC", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
private void showNotification(String message) {
Intent i=new Intent(this, SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM TITLE").setContentText(message)
.setSmallIcon(R.drawable.pt_icon)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我甚至没有在上面的notification.java中登录.
Project.gradle
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com/' …Run Code Online (Sandbox Code Playgroud)