当前位置: 首页 > 知识库问答 >
问题:

如果应用程序有背景,则Firebase推送通知会转到错误页面

尉迟鑫鹏
2023-03-14

我已经创建了发送推送通知的应用程序。一切正常。如果应用程序正在运行或未关闭,它会直接将我带到第二页。但问题是,运行我的应用程序后,如果我把我的应用程序在后台或关闭,然后发送通知。点击notification后,我会进入主页面,而不是第二页。我想它必须带我到第二页,即使应用程序关闭或在后台。下面是我的代码:

public class MainActivity extends Activity {

    private static final String TAG="MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button BtnshowToken=(Button)findViewById(R.id.buttonshowtoken);
        BtnshowToken.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String token= FirebaseInstanceId.getInstance().getToken();
                Log.d(TAG,"Token: "+token);
                Toast.makeText(MainActivity.this,token,Toast.LENGTH_SHORT).show();

            }
        });
    }


}

MyFireBaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG="MyFirebaseInsIDService";

    @Override
    public void onTokenRefresh() {

        //get updated token
        String refreshedToken= FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG,"New Token: "+refreshedToken);



    }
}


MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService{
    private static final String TAG="MyFirebaseMsgService";


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       Log.d(TAG, "FROM:" +remoteMessage.getFrom());

        //Check if msg contains data
        if(remoteMessage.getData().size() > 0){
            Log.d(TAG, "Message data: "+remoteMessage.getData());
        }


        //Check if msg contains notification
        if(remoteMessage.getNotification()!=null){
            Log.d(TAG,"Message body: "+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }

        //Display notification
private void  sendNotification(String body){

    Intent intent=new Intent(this,SecondActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
    //Set sound of notification
    Uri notificationsound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Cloud Messaging")
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(notificationsound)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0/*Id of notification*/,notifiBuilder.build());



    }
}


SecondActivity.java
public class SecondActivity extends Activity {

     TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);


    }
}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">





    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="151dp"
        android:src="@drawable/firebase"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Token"
        android:id="@+id/buttonshowtoken"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="49dp" />
</RelativeLayout>


second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:id="@+id/tv2"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:text="Second Activity"
    android:textColor="#000"
    android:gravity="center"
    android:textSize="30dp"/>

    <ImageView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image"
        android:src="@drawable/welcome"
        />

</LinearLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abc.firebasenoteg" >


    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="YourActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

        <service
            android:name=".MyFirebaseMessagingService"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />


            </intent-filter>
        </service>

        <service
            android:name=".MyFirebaseInstanceIDService"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />


            </intent-filter>
        </service>



    </application>

</manifest>


google-services.json
{

  "project_info": {
    "project_number": "785125209715",
    "firebase_url": "https://fir-notification-a5fce.firebaseio.com",
    "project_id": "fir-notification-a5fce",
    "storage_bucket": "fir-notification-a5fce.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:785125209715:android:676ef33a2e673fd3",
        "android_client_info": {
          "package_name": "com.example.abc.firebasenoteg"
        }
      },
      "oauth_client": [
        {
          "client_id": "785125209715-496loih2csg0egp7h9hphtiaui49coeb.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyCdvlwxewCNStJwTwcqq3CGBGZ08j9MVdU"
        }
      ],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "ads_service": {
          "status": 2
        }
      }



    }
  ],


  "configuration_version": "1",



}

共有1个答案

容磊
2023-03-14

您需要从服务器请求中单击listener,并在活动内部的manifest intent-action中添加listener名称。

"notification" : {
"body" : "Body",
"title" : "Titme",
"icon" : "YourIcon"
"click_action" : "<<yourkey e.g. MainActivity>>"
}

<activity
     android:name=".MainActivity">
     <intent-filter>
        <action android:name="<<yourkey e.g. MainActivity>>" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

这是当您的应用程序不可用的前景。希望这对你有帮助,谢谢

 类似资料:
  • null 我还试了一下: 关闭应用程序进程-&>;关闭设备-&>;打开设备电源-&>;发送通知。。。并且没有收到通知! 看起来firebase只有在设备启动了应用程序而不是100%关闭的情况下才能收到通知,我的意思是,只需用后退键关闭它而不是关闭应用程序进程。 这怎么可能呢?据推测,即使应用程序关闭,firebase也应该收到通知。 我正在Nexus5X和Android 8.0上进行测试,我使用的

  • 我想在我的应用程序处于后台时显示推送通知 我正在使用flatter\u local\u通知包和firebase\u消息包。 推送通知在firebase_消息和我的应用程序为后台时非常有效 但是,以下方法: 如果通过传递对象,则不会调用: 因此,我必须传递对象,它是一个

  • 我无法发送通知 {“多播id”:2200169071930988341,“成功”:0,“失败”:1,“规范id”:0,“结果”:[{“错误”:“无效注册”}]

  • 我知道这个问题已经问过很多次了,但是我仍然无法从parse.com获得推送通知 推送通知正在从parse.com成功发送,但我的应用程序没有接收到它们。 大多数人用这个解决了他们的问题:我不能在应用程序中接收来自对我不起作用的Parse bt的推送通知。 我也试过Android--不能接收来自parse.com的推送。 有些人建议改变包的名称,我做了,甚至做了一个新的应用程序,但没有帮助。 这是我

  • 我在Android项目中使用推送通知(GCM)。 根据GCM教程,我实现了广播接收器,并将其注册到。 这种广播接收器应该接收消息,即使我的应用程序是关闭的(不仅如果我的应用程序是在后台,但即使它是强制停止)。 但它并不像我预期的那样有效<如果应用程序关闭,则不会调用code>onReceive()方法。看来我对广播接收机的理解是正确的,问题在于我对GCM的期望。 其中一个可能的原因是,如果应用程序

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中