Android后台通知无法打开应用

问题描述

我正在从我的PHP调用FCM(不必担心PHP),并且它具有click_action =>'OPEN_APP',这意味着,至少在应用程序处于后台或已终止的情况下,通知应打开应用程序启动器活动,但实际上并没有。同时,如果应用程序处于前台状态,则通知有效负载可以正常工作,并且未决意图可以打开应打开的内容

来自manifest.xml代码

<activity
        android:name=".SplashScreen"
        android:configChanges="keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="OPEN_APP" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
//other activities
<service
        android:name=".services.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
        <Meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/via" />
        <Meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimaryDark" />
        <Meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/newsFeed_channel_id" />
    </service>
    <service
        android:name=".receivers.OnBootbroadcastReceiver"
        android:exported="true"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </service>

来自MyFirebaseMessagingService.class代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG,"Message Notification Body: " + remoteMessage.getNotification().getBody());

            //get key/value from notification
            String lectureDate = remoteMessage.getData().get("lecture_date");
            String web_url = remoteMessage.getData().get("web_url");

            if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),lectureDate);
            else Log.e(TAG,"Message.notification is empty!");
        }
    }

    @Override
    public void onNewToken(@NonNull String token) {
        Log.d(TAG,"Refreshed token: " + token);
    }

    private void sendLecturesNotification(String title,String messageBody,String date) {
        int NOTIFICATION_ID = (int) System.currentTimeMillis();
        Intent intent;
        intent = new Intent(this,Lectures_graph.class).putExtra("lecture_date",date)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,NOTIFICATION_ID,intent,0);

        String channelId = getString(R.string.lectures_channel_id);
        Uri defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this,channelId)
                        .setSmallIcon(R.drawable.via)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setCategory(NotificationCompat.CATEGORY_REMINDER)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setContentIntent(pendingIntent);

        notificationmanager notificationmanager =
                (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (notificationmanager != null) {
            notificationmanager.notify(NOTIFICATION_ID,notificationBuilder.build());
        }

        Log.d(TAG,"Lectures notification built with date:"+date);

    }
}

我很好奇,是否可以使用时间限制作为通知ID,因为它们每个都需要唯一的ID?

int NOTIFICATION_ID = (int) System.currentTimeMillis();

来自OnBootbroadcastReceiver.class代码

public class OnBootbroadcastReceiver extends broadcastReceiver {
    @Override
    public void onReceive(Context context,Intent intent) {
        Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
        i.setClass(context,MyFirebaseMessagingService.class);
        context.startService(i);
    }
}

来自Splashscreen.class代码

public class SplashScreen extends AppCompatActivity {

    //length of splashscreen
    private static int SPLASHSCREEN_LENGTH = 1250;
    private String TAG = "MenuScreen";

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

        findViewById(R.id.welcome_screen).startAnimation(AnimationUtils.loadAnimation(this,R.anim.appear));

        Handler handler = new Handler();

        //closes splashscreen after given time
        handler.postDelayed(new Runnable() {
            public void run() {
                //close welcome screen after given time
                Bundle extras = getIntent().getExtras();
                if(extras != null) {
//im handling notification extras here
                }
                finish();
            }
        },SPLASHSCREEN_LENGTH);
    }
}

那么我可能在哪里犯了错误

我有工作中的FCM接收器,工作中的通知生成器。 我的启动器活动具有与PHP代码中提供的click_action相同的意图动作。 前景一切正常。 我什至在BOOT_COMPLETED上开始了FCM接收服务,以便随时接收消息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...