Android FCM работает частично, пока приложение не работает - PullRequest
0 голосов
/ 03 августа 2020
• 1000 *

Чего я хочу достичь: полностью работающие уведомления FCM, даже когда приложение убито, уведомления должны открывать активность (с дополнительными функциями) после их нажатия.

Как далеко я: я могу получать уведомления об открытии Lectures_graph.class с дополнительными функциями, все загружается и работает нормально. Даже если я получаю уведомления об активности webview_base.class, при нажатии на уведомление ничего не происходит.

Может быть одна вещь, указывающая на проблему - Logcat часто говорит мне это:

E/FirebaseMessaging: Notification pending intent canceled

Я использую PHP для отправки уведомлений (этот код на 100% правильный и работает), код ниже показывает идею отправки JSON для активности webview_base.class. Чтобы отправлять уведомления об активности Lectures_graph.class, мне просто нужно поменять местами click_action с «OPEN_ACTIVITY_WEBVIEW» на «OPEN_ACTIVITY_LECTURES» и изменить полезные данные. PHP для активности webview_base.class отправляет такие данные:

$msg = array
    (
        'body'  => 'test body',
        'title'     => "test title",
        'click_action' => 'OPEN_ACTIVITY_WEBVIEW',
        'channelId' => "newsfeed",
        'vibrate'   => 1,
        'sound'     => 1
        
    );


//sends notifications via topics
$fields = array
    (
        "data" => [
            'web_url' => $_POST['notification_url']
        ],
        'notification'          => $msg,
        'to'  => '/topics/test'
    );

Это весь мой файл манифеста из Android studio:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="secret">
    
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />
    
    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme.CustomTheme"
        android:usesCleartextTraffic="true"
        tools:targetApi="m">
        <activity
            android:name=".systems.webview_activity.webview_base"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <category android:name="android.intent.category.BROWSABLE" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="file" />
                <data android:mimeType="\*/\*" />
                <data android:pathPattern=".*\\.kdb" />
                <data android:host="*" />
    

//even if this method worked with Lectures_graph activity
//it doesnt work with this one

                <action android:name="OPEN_ACTIVITY_WEBVIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".systems.lectures.Lectures_graph"
            android:configChanges="keyboardHidden|screenSize"
            android:label="@string/title_lectures_graph"
            >
            <intent-filter>

//this intent filter provides correct response
//to click_action which is provided in my PHP file


                <action android:name="OPEN_ACTIVITY_LECTURES" />
                <action android:name = "android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SplashScreen"
            android:configChanges="keyboardHidden|orientation|screenSize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity
            android:name=".NoInternetConnection"
            android:configChanges="keyboardHidden|orientation|screenSize" />
        <activity
            android:name="secret.systems.about.about_app"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/FullscreenTheme" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:configChanges="orientation|keyboardHidden|screenSize">
    
        </activity>
    
        <service
            android:name="secret.services.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
    
    </manifest>

MyFirebaseMessagingService. java:

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 if(web_url != null) sendNotificationWithURL(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), web_url);
            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) {

        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, ((int) System.currentTimeMillis()) /* Request code */, 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.secret)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (notificationManager != null) {
            notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
        }

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

    private void sendNotificationWithURL(String title, String messageBody, String web_url) {

        Intent intent;
        intent = new Intent(this, webview_base.class).putExtra("web_url", web_url).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, ((int) System.currentTimeMillis()) /* Request code */, intent,
                0);

        String channelId = getString(R.string.newsfeed_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.secret)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (notificationManager != null) {
            notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
        }

        Log.d(TAG, "Webview notification built with URL:"+web_url);
    }
}

некоторое время go я получал сообщение об ошибке в logcat, в котором говорилось что-то вроде «канал FCM по умолчанию не определен», но я не совсем уверен, что это проблема.

Даже если бы я поискал по всей сети, я бы хотел увидеть лучшие решения / предложения для обработки уведомлений (с их полезной нагрузкой), когда приложение убито.

1 Ответ

0 голосов
/ 04 августа 2020

Итак, я исправил свой кошмар, и вот как:

Поскольку я не могу открыть более одного c действия из уведомлений, почему я могу просто открыть основной класс своих приложений и обработать оттуда дополнительную переменную?

Вместо того, чтобы иметь для каждого желаемого действия

   <intent-filter>
        <action android:name="OPEN_ACTIVITY_???????" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

, я сделал следующее:

<activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:configChanges="orientation|keyboardHidden|screenSize">
    <intent-filter>
        <action android:name="OPEN_APP" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

Я добавил дополнительную переменную в полезную нагрузку уведомлений (например: 'activity' => 'webview') и переименовал click_action в моем коде PHP. Я обработал все полезные данные в MainActivity.class и вызвал оттуда действие android. Код MainActivity:

Bundle extras = getIntent().getExtras();
        if(extras == null) {
            Log.e("MainActivity", "No Activities passed in notification extras");
        } else {
            //get opening activity from notification payload
            String activity = extras.getString("activity");
            if( activity != null){
                switch (activity){
                    //webview activity
                    case "webview":
                        //gets url for webview
                        String web_url = extras.getString("web_url");
                        if(web_url != null){
                            finish();
                            startActivity(new Intent(this, webview_base.class).putExtra("web_url", web_url));
                        }
                        break;
                    //lectures activity
                    case "lectures":
                        //gets lecture date
                        String lecture_date = extras.getString("lecture_date");
                        if(lecture_date != null){
                            finish();
                            startActivity(new Intent(this, Lectures_graph.class).putExtra("lecture_date", lecture_date));
                        }
                        break;
                }
            }
        }

Надеюсь, когда-нибудь это кому-нибудь поможет ..

Ура.

...