android - 如何修复应用程序运行时不显示通知?

标签 android firebase firebase-notifications

我制作了一个实现了 Firebase 的测试应用程序,我正在使用 Firebase 的通知编辑器,它工作正常但不符合预期,因为通知仅在应用程序未处于 Activity 状态(在屏幕上)时显示,并且我即使应用程序处于 Activity 状态并正在运行,也需要它显示

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>

编辑:添加 list

最佳答案

当应用关闭时,您的通知将由 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);
   }
 }

并将其添加到您的 list 文件

<service android:name=”.NotificationService”>
  <intent-filter>
    <action android:name=”com.google.firebase.MESSAGING_EVENT”/>
  </intent-filter>
</service>

还要确保您在 list 文件中给予适当的权限。 实现重试后,您应该会在应用处于前台时看到通知。

关于android - 如何修复应用程序运行时不显示通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55446687/

相关文章:

android - 从 Google Fit API Android 获取当前 Activity

java - Android内存泄漏异常

javascript - Firebase 错误 Firebase 存储 : Invalid argument in `put` at index 0: Expected Blob or File

ios - 如何在 iOS 中使用 Google Firebase 向特定用户发送推送通知?

javascript - 如何在无人在线的情况下保持数据库自动更新(firebase)?

ios - 函数 didReceive RemoteMessage 未被调用 Swift 5

android - 如果应用程序关闭,则不会收到 Firebase 推送通知

java - Recyclerview动态textview高度

当应用程序从多任务托盘停止时,Android 应用程序未收到 Firebase 通知

java - RecyclerView 空指针异常