在Eclipse中没有收到使用Firebase的通知 [英] Not getting notification using Firebase in Eclipse

查看:249
本文介绍了在Eclipse中没有收到使用Firebase的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了Firebase的构建,而没有使用Eclipse的任何警告(参见相关内容:无法在Eclipse中找到混淆的Firebase类



我尝试发送测试通知,但是我没有得到任何东西。我也在使用图书馆项目。这是我的代码:



AndroidManifest.xml

 <应用程序/> 
...
<! - ==============================
FIREBASE
================================= - >
< service
android:name =。MyFirebaseMessagingService
android:exported =false>
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>

< service
android:name =。MyFirebaseInstanceIdService
android:exported =false>
< intent-filter>
< action android:name =com.google.firebase.INSTANCE_ID_EVENT/>
< / intent-filter>
< / service>
....
< / application>

这些文件几乎直接来自样本:



public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String PUSH_NOTIFICATION_TEXT =pushnotificationtext;

private static final String TAG =Firebase;

public static final int NOTIFICATION_ID = 1;

@Override
public void onMessageReceived(RemoteMessage remoteMessage){
//处理FCM消息的数据有效负载。
String messageId = remoteMessage.getMessageId();
RemoteMessage.Notification notification = remoteMessage.getNotification();
映射< String,String> data = remoteMessage.getData();

Log.d(TAG,FCM Message Id:+ messageId);
Log.d(TAG,FCM通知消息:+通知);
Log.d(TAG,FCM数据消息:+数据);

sendNotification(this,notification.toString());

}

//将GCM消息放入通知并发布。
private void sendNotification(Context context,String message){

NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

final Intent intent = new Intent(context,Splash.class);
//intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

//保存推送通知消息以在应用程序启动时显示
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.edit()。putString(PUSH_NOTIFICATION_TEXT,message).commit();

PendingIntent contentIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_ONE_SHOT);

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setVibrate(new long [] {0,500,200,500, 200,500})
.setContentIntent(contentIntent)
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_launcher)
。 setStyle(new NotificationCompat.BigTextStyle()。bigText(message))
.setContentText(message)
.setWhen(System.currentTimeMillis())。setOngoing(false);

mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
}

}

其他类:

  public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

private static final String TAG =Firebase;
private static final String FRIENDLY_ENGAGE_TOPIC =friendly_engage;

/ **
*应用程序的当前实例ID令牌不再有效
*,因此必须请求新的实例ID令牌。
* /
@Override
public void onTokenRefresh(){
//如果需要处理生成的令牌,最初或
//刷新后这是你应该这样做的地方。
String token = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,FCM令牌:+令牌);

//生成一个令牌后,我们订阅主题。
FirebaseMessaging.getInstance()。subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
}
}

我通过针对包的Firebase控制台发送消息名字没有运气。我不确定我应该做什么来让这个工作。我需要以某种方式使用 google-services.json 文件?



谢谢

解决方案

我不使用Eclipse,我无法验证这些步骤是否有效。我希望他们能让你从正确的路径开始。



Firebase框架由 FirebaseInitProvider 。您不需要实现或子类化。只需在您的清单中声明。

 < provider 
android:authority =$ {applicationId} .firebaseinitprovider
android:name =com.google.firebase.provider.FirebaseInitProvider
android:exported =false
android:initOrder =100/>

FirebaseInitProvider 期望找到包含项目的配置值。在使用Android Studio制作的应用程序和 Google服务Gradle插件中,资源值是从 google-services.json 文件创建的。因为您没有使用AS和插件,您需要在 res 文件夹中定义这些资源。这些值可以在Firebase控制台的项目设置或 google-service.json文件中找到。因为你只对消息传递感兴趣,所以可能不需要一些值。为了安全起见,先定义它们。

 <?xml version =1.0encoding =utf-8? > 
< resources>
< string name =default_web_client_idtranslatable =false> 8888888888888-ooqodhln4cjj4qst7b4sadfiousdf7.apps.googleusercontent.com< / string>
< string name =firebase_database_urltranslatable =false> https://project-888888888888888.firebaseio.com< / string>
< string name =gcm_defaultSenderIdtranslatable =false> 888888888888< / string>
< string name =google_api_keytranslatable =false> AIzaSyB0Bhr1sfsydfsdfnwelhkOYifak_Go2xU< / string>
< string name =google_app_idtranslatable =false> 1:888888888888:android:526f9740dfg987sdfg< / string>
< string name =google_crash_reporting_api_keytranslatable =false> AIzaSyDkG-g8hH7T4TV7Rrsdfgiopudfmn234897< / string>
< string name =google_storage_buckettranslatable =false> project-8888888888888888888888.appspot.com< / string>
< / resources>

您可以确认初始化成功,将此代码放在 onCreate )

  FirebaseOptions opts = FirebaseApp.getInstance()。getOptions() ; 
Log.i(TAG,onStart:ID =+ opts.getApplicationId());
Log.i(TAG,onStart:SenderId =+ opts.getGcmSenderId());
Log.i(TAG,onStart:Key =+ opts.getApiKey());

如果记录有效的结果,它表示默认的 FirebaseApp 已初始化,您应该能够接收邮件。


I've got Firebase building without any warnings using Eclipse (see related: Unable to find obfuscated Firebase class in Eclipse)

I try to send a test notification, however I'm not getting anything. I'm using a library project as well. This is my code:

AndroidManifest.xml

<application />
    ... 
    <!-- ==================================
               FIREBASE
    =================================== -->
    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

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

These files are practically straight from the samples:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String PUSH_NOTIFICATION_TEXT = "pushnotificationtext";

    private static final String TAG = "Firebase";

    public static final int NOTIFICATION_ID = 1;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle data payload of FCM messages.
        String messageId = remoteMessage.getMessageId();
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();

        Log.d(TAG, "FCM Message Id: " + messageId);
        Log.d(TAG, "FCM Notification Message: " + notification);
        Log.d(TAG, "FCM Data Message: " + data);

        sendNotification(this, notification.toString());

   }

    // Put the GCM message into a notification and post it.
    private void sendNotification(Context context, String message) {

        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        final Intent intent = new Intent(context, Splash.class);
        //intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        //Save push notification message to show when app starts
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putString(PUSH_NOTIFICATION_TEXT, message).commit();

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setVibrate(new long[] { 0, 500, 200, 500, 200, 500 })
            .setContentIntent(contentIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message)
            .setWhen(System.currentTimeMillis()).setOngoing(false);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

}

Other class:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "Firebase";
    private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage";

    /**
    * The Application's current Instance ID token is no longer valid 
    * and thus a new one must be requested.
    */
    @Override
    public void onTokenRefresh() {
        // If you need to handle the generation of a token, initially or
        // after a refresh this is where you should do that.
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "FCM Token: " + token);

        // Once a token is generated, we subscribe to topic.
        FirebaseMessaging.getInstance().subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
    }
}

I send a message via the Firebase console targeting the package name with no luck. I'm uncertain what I should do to get this to work. Do I need to use the google-services.json file in some way?

Thanks

解决方案

I don't use Eclipse and am not able to verify that these steps work. I hope they will get you started on the right path.

The Firebase framework is initialized by FirebaseInitProvider. You don't need to implement or subclass it. Just declare it in your manifest.

    <provider
        android:authorities="${applicationId}.firebaseinitprovider"
        android:name="com.google.firebase.provider.FirebaseInitProvider"
        android:exported="false"
        android:initOrder="100" />

FirebaseInitProvider expects to find string resources that contain the configuration values for the project. In an app built with Android Studio and the Google Services Gradle Plugin, the resource values are created from the google-services.json file. Because you are not using AS and the plugin, you will need to define these resources in your res folder. The values can be found in your Project Settings at the Firebase Console or in the google-service.json file. Because you are only interested in messaging, some values may not be needed. To be safe, define them all initially.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id" translatable="false">8888888888888-ooqodhln4cjj4qst7b4sadfiousdf7.apps.googleusercontent.com</string>
    <string name="firebase_database_url" translatable="false">https://project-888888888888888.firebaseio.com</string>
    <string name="gcm_defaultSenderId" translatable="false">888888888888</string>
    <string name="google_api_key" translatable="false">AIzaSyB0Bhr1sfsydfsdfnwelhkOYifak_Go2xU</string>
    <string name="google_app_id" translatable="false">1:888888888888:android:526f9740dfg987sdfg</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzaSyDkG-g8hH7T4TV7Rrsdfgiopudfmn234897</string>
    <string name="google_storage_bucket" translatable="false">project-8888888888888888888888.appspot.com</string>
</resources>

You can confirm that the initialization succeeded by putting this code in the onCreate() method of your main activity:

    FirebaseOptions opts = FirebaseApp.getInstance().getOptions();
    Log.i(TAG, "onStart: ID=" + opts.getApplicationId());
    Log.i(TAG, "onStart: SenderId=" + opts.getGcmSenderId());
    Log.i(TAG, "onStart: Key=" + opts.getApiKey());

If valid results are logged, it indicates the default FirebaseApp has been initialized, and you should be able to receive messages.

这篇关于在Eclipse中没有收到使用Firebase的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆