Firebase通知未在Android设备上显示

时间:2016-10-26 09:35:31

标签: android firebase firebase-cloud-messaging

我正在尝试开发一个使用Firebase显示通知的模块。该模块具有订阅和取消订阅主题的3功能。它还具有firebase sample

中提到的两项服务

我知道它正在运行,因为它正在与Firebase控制台同步主题。正在创建新主题。

此模块已连接到使用它的主应用程序模块。

我的问题是,当发送通知时,我没有收到。

Firebase模块代码:

Android清单

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <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="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!-- for plugin -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true">


        <service
            android:name=".AbleFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">

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


        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest> 

AbleFirebaseMessagingService

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.ineqe.able.library.MainActivity;

public class AbleFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "AbleFirebaseService";
        @Override
        public void onMessageReceived (RemoteMessage remoteMessage){
            // [START_EXCLUDE]
            // There are two types of messages data messages and notification messages. Data messages are handled
            // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
            // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
            // is in the foreground. When the app is in the background an automatically generated notification is displayed.
            // When the user taps on the notification they are returned to the app. Messages containing both notification
            // and data payloads are treated as notification messages. The Firebase console always sends notification
            // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
            // [END_EXCLUDE]

            // TODO(developer): Handle FCM messages here.
            // Not getting messages here? See why this may be: 
            Log.d(TAG, "From: " + remoteMessage.getFrom());

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

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

            // Also if you intend on generating your own notifications as a result of a received FCM
            // message, here is where that should be initiated. See sendNotification method below.
        }
        // [END receive_message]

        /**
         * Create and show a simple notification containing the received FCM message.
         *
         * @param messageBody FCM message body received.
         */

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    }

FirebaseIDService

package com.ineqe.firebasenotification;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

/**
 * Created by brendan on 26/10/2016.
 */

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     * <p>
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

FireBasePushService

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.ineqe.able.library.PushService;

/**
 * Created by brendan on 25/10/2016.
 */

public class FireBasePushService implements PushService {
    @Override
    public void initialize() {

        Log.v("Token", FirebaseInstanceId.getInstance().getToken());
        FirebaseMessaging.getInstance().subscribeToTopic("global");
    }

    @Override
    public void addChannel(String channel) {
        FirebaseMessaging.getInstance().subscribeToTopic(channel);
    }

    @Override
    public void removeChannel(String channel) {

        FirebaseMessaging.getInstance().unsubscribeFromTopic(channel);
    }
}

此外,我将添加manifest,gradle.build文件和主活动文件,以显示我如何引用firebase模块。

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ineqe.hsct.fostering">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <permission
     android:name="com.ineqe.hsct.fostering.permission.C2D_MESSAGE"
     android:protectionLevel="signature" />
     <uses-permission android:name="com.ineqe.hsct.fostering.permission.C2D_MESSAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name=".MyApplication"
        android:theme="@style/AppTheme">



         <meta-data
             android:name="com.google.firebase.messaging.default_notification_icon"
             android:resource="@drawable/ic_not" />

        <activity
            android:name=".WalkthroughActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".StafDirectoryDetailActivity"
            android:theme="@style/AppTheme2" />
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.MyApp"
            android:configChanges="screenSize|orientation"
            android:label="@string/app_name">

        </activity>
        <activity
            android:name=".ExpansionActivity"
            android:configChanges="screenSize|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

MyApplication.java

import com.ineqe.firebasenotification.FireBasePushService;

public class MyApplication extends com.ineqe.able.library.MyApplication {

    @Override
    public void onCreate() {
        setMainActivity(MainActivity.class);
        setTintAppColor("#00b5cc");
        setSplashImage("hsctf_splash_bg");
        setAppTracker("UA-42956664-12");
        FireBasePushService ps = new FireBasePushService();
        ps.initialize();
        setPushService(ps);
        setLoginImage("hsctf_login_bg");
        setSchool(true);
        setTesting(false);
        setAnswersPage(true);
        setExpansionFiles(false);
        super.onCreate();

    }


}

Gradle.build

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.ineqe.hsct.fostering"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        versionCode 19
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }

}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'me.relex:circleindicator:1.2.1@aar'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile project(':able')
    compile project(":firebasenotification")


}

我现在所知道的是:

  1. 该应用可以与firebase控制台通信并创建新主题。
  2. 即使我将令牌用于我正在使用的特定设备,通知似乎也没有到达。
  3. 如果您有任何想法,请不要犹豫,发表评论

2 个答案:

答案 0 :(得分:1)

我弄清楚我的错误在哪里。这一行:

apply plugin: 'com.google.gms.google-services'
应该将

添加到app模块的build.gradle中,而不是firebase模块。此外,在创建项目时,正确的方法是输入app模块的包,并将google-services.json文件保存在app模块而不是其他firebase模块中。

要重复使用firebase模块(如果有的话),请务必按照前面的说明进行操作。

答案 1 :(得分:0)

首先在firebaseMessanging服务中添加此过滤器:

<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>

在firebaseMessanging中:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        RemoteMessage.Notification notification = remoteMessage.getNotification();

        Map<String, String> data = remoteMessage.getData();
       ShowNotification(notification, data);
    }

    private void ShowNotification(RemoteMessage.Notification notification, Map<String, String> data) {

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/notification");


        android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(data.get("title"))
                .setContentText(data.get("text"))
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pendingIntent)
                .setContentInfo("ANY")
                .setLargeIcon(icon)
                .setColor(Color.RED)
                .setSmallIcon(R.mipmap.ic_launcher);


        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        notificationBuilder.setLights(Color.YELLOW, 1000, 300);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }

在FIrebaseIDservice:

@Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        FirebaseMessaging.getInstance().subscribeToTopic("GLOBALgroup");

       // sendRegistrationToServer(refreshedToken);   Your METHOD to save TOKENS
    }