android - 如何解决应用程序运行时不显示通知的问题?
问题描述
我制作了一个实现了 Firebase 的测试应用程序,我正在使用 Firebase 的通知编写器,它可以正常工作但没有达到预期的效果,因为通知仅在应用程序未处于活动状态(在屏幕上)并且我需要它时显示即使应用程序处于活动状态并正在运行,也会显示
MainActivity.java
package com.gci.gestioncapteursincendie;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= findViewById(R.id.textViewToken);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(task.isSuccessful()){
String token = task.getResult().getToken();
System.out.println("token:"+token);
textView.setText("Token : " + token);
}
else
{
textView.setText("Token Not Generated");
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gci.gestioncapteursincendie">
<!-- Granting Internet access permission to app -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.gci.gestioncapteursincendie.FirebaseActivity"
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">
<service
android:name=".FirebaseMessagingServiceExtended"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
编辑:添加清单
解决方案
当应用程序关闭时,您的通知将由 Google 服务进程处理,该进程负责根据需要显示您的通知,包括默认点击操作(打开应用程序)和通知图标。
当应用程序在前台时,收到的消息由应用程序处理,由于没有逻辑来处理它,所以什么都不会发生!
您可以创建一个FirebaseMessagingService
实现的自定义,onMessageReceived
然后在其中创建通知..
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(some_int_number (eg : 123), notification);
}
}
并将其添加到您的清单文件中
<service android:name=”.NotificationService”>
<intent-filter>
<action android:name=”com.google.firebase.MESSAGING_EVENT”/>
</intent-filter>
</service>
还要确保您在清单文件中给予适当的许可。实施重试后,您应该会在应用处于前台时看到通知。
推荐阅读
- javascript - Electron - dialog.showOpenDialogSync() 不允许文件和文件夹
- sql - 获取组的前 5 列
- swift - 导航栏未绑定到顶部 swift
- java - 如何以随机顺序调用方法(Java JDK11)?
- java - 更新 GUI 以响应模型更改
- javascript - 将文本添加到 openlayers 地图的最简单示例?
- python-3.x - OpenAPI 3 python-flask:如何在响应中使用多种内容类型
- amazon-web-services - 查找重新启动的 EC2 实例的来源
- batch-file - IF EXIST 'path' 在 FOR /F 中不起作用
- regex - 正则表达式:将字符串拆分为 1 个或多个组