0 Frage: Warum werden meine in FirebaseMessagingService empfangenen onMessageReceived nicht ausgeführt?

Frage erstellt am Wed, May 8, 2019 12:00 AM

Ich versuche, eine App zum Empfangen einer Benachrichtigung von Firebase Cloud Messaging zu erstellen. Aber es funktioniert nicht:

Anscheinend wird die onMessageReceived nicht ausgeführt. Ich versuche, ein Protokoll zu schreiben, um die Nachricht zu sehen, wenn ich sie von der Firebase an die App sende, aber ich kann nichts sehen. Dies ist mein Code:

Meine MyFirebaseMessagingService-Klasse:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //super.onMessageReceived(remoteMessage);
        Log.d("From: " , remoteMessage.getFrom());
        if(remoteMessage.getNotification() != null){
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            Log.d("title: ", title);
            Log.d("body: ", body);
            sendNotification(title,body);

        }

    }

    @Override
    public void onNewToken(String s) {
        Log.d("NewToken: ",s);

        sendRegistrationToSever(s);
    }

    private void handleNow(){

    }
    private void sendRegistrationToSever(String token){

    }

    private void sendNotification(String title, String body){
        String channelId = getString(R.string.defaul_notification_channel_id);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = (PendingIntent) PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channelId,"Firebase Noti", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(notificationChannel);

        }
        notificationManager.notify(0, builder.build());
    }
}

Mein Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dfk.example.com.firebasenotificationtry3">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <application
        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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".Service.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "dfk.example.com.firebasenotificationtry3"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.google.gms:google-services:4.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
    
0
  1. Verwenden Sie Cloud-Funktionen, um die Daten an Ihre Serviceklasse zu senden?
    2019-05-08 09: 21: 29Z
  2. Warum wird super.onMessageReceived (remoteMessage) kommentiert?
    2019-05-08 09: 22: 14Z
  3. @ PeterHaddad Ich verwende die Firebase-Site, um es zu senden
    2019-05-08 09: 33: 37Z
  4. @ HariKrishnan versuche es und kommentiere, dass es nicht beide funktioniert
    2019-05-08 09: 34: 04Z
  5. Befindet sich Ihre App im Hintergrund Ihres Telefons?
    2019-05-08 09: 43: 43Z
0 Antworten                              0                         
Quelle platziert Hier