2017-02-17 94 views
1

我的MessageRecieved方法永远不会在我的Android应用程序中被解雇,尽管服务正在运行并且清单似乎没有问题。我们通过Firebase控制台发送消息,似乎没有任何事情发生。此外,onToken刷新方法只会被调用一次,这是您第一次安装应用程序时。之后,它永远不会被调用。我已经在前台和后台测试了应用程序,但仍未调用onMessageRecieve方法。onMessageRecieved从未解雇Firebase通知

这里是我的清单

 <?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="com.example.beatrice.mylocalbartender"> 

     <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme" 
      tools:node="replace" 
      tools:replace="android:supportsRtl"> 

      <!-- meta data to connect ot the facebook api --> 

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

      <service 
       android:enabled="true" 
       android:exported="true" 
       android:name=".messaging.MyFirebaseMessagingService"> 
       <intent-filter> 
        <action android:name="com.google.firebase.MESSAGING_ 
    EVENT"/> 
       </intent-filter> 
      </service> 


      <meta-data 
       android:name="com.google.firebase.messaging.default_notification_icon" 
       android:resource="@drawable/bt_ic_android_pay" /> 
      <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming 
       notification message. for more. --> 
      <meta-data 
       android:name="com.google.firebase.messaging.default_notification_color" 
       android:resource="@color/colorAccent" /> 


      <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> 
      <meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true"/> 
      <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 




      <uses-permission android:name="android.permission.INTERNET" /> 

      <activity android:name=".messaging.MainActivity"></activity> 
      <activity android:name=".activity.GmailSignInActivity"> 

      </activity> 

      <activity android:name=".activity.MainActivity"> 

       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 

      </activity> 

      <!-- activity for brain tree, please do not touch --> 
      <activity 
       android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity" 
       android:launchMode="singleTask"> 
       <intent-filter> 
        <action android:name="android.intent.action.VIEW" /> 

        <category android:name="android.intent.category.DEFAULT" /> 
        <category android:name="android.intent.category.BROWSABLE" /> 

        <data android:scheme="${applicationId}.braintree" /> 
       </intent-filter> 
      </activity> 

      <!-- This activity will be removed in the future --> 

      <activity 
       android:name=".activity.FacebookLogInActivity" 
       android:label="@string/title_activity_facebook_log_in" 
       android:theme="@style/AppTheme.NoActionBar"> 

      </activity> 


      <!-- to launch facebook activity - please do not touch --> 

      <activity android:name="com.facebook.FacebookActivity" 
       android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
       android:theme="@android:style/Theme.Translucent.NoTitleBar" 
       android:label="@string/app_name" /> 

      <activity enter code hereandroid:name="com.braintreepayments.api.dropin.DropInActivity"></activity> 
      <activity android:name="com.braintreepayments.api.dropin.AddCardActivity"></activity> 



     </application> 

    </manifest> 

正如你可以看到服务已启用,并出口。 下面是服务

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService { 

    private static final String TAG = "MyFirebaseIIDService"; 
    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. 
    */ 

    public MyFirebaseInstanceIdService(){ 
     super(); 
     Log.d("startedServiceNoLie","hiiiiiiiiiiiiiiiiiiiii"); 
    } 


    @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); 
    } 
} 

这是FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFMService"; 

    public MyFirebaseMessagingService() { 
     super(); 
     Log.d("startedMessageRecieved","messagingService"); 

    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // Handle data payload of FCM messages. 
     Log.d("messageRecieved", "FCM Message Id: " + remoteMessage.getMessageId()); 
     Log.d("messageRecieved", "FCM Notification Message: " + 
       remoteMessage.getNotification()); 
     Log.d("messageRecieved", "FCM Data Message: " + remoteMessage.getData()); 
    } 
} 

这是从那里既是服务正在从

public class MainActivity extends AppCompatActivity { 

    Button launch_msg_app; 
    // private static DatabaseReference root = FirebaseDatabase.getInstance().getReference(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     FirebaseApp.initializeApp(this); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.deletethislayout); 

     Intent intent = new Intent(this, MyFirebaseInstanceIdService.class); 

     Log.d("tokenNotRefresh",FirebaseInstanceId.getInstance().getToken()); 
     startService(intent); 


     startService(new Intent(this, MyFirebaseMessagingService.class)); 

     //startService(new Intent(this, DeleteService.class)); 
    } 

这里是我的项目,称为活动。 gradle这个

buildscript { 
    repositories { 
     jcenter() 
     maven { url 'https://maven.fabric.io/public' } 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.2.3' 
     classpath 'com.google.gms:google-services:3.0.0' 
     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
     mavenLocal() 
     maven { 
      url 'https://maven.fabric.io/public' 
     } 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

这里是我的应用程序.gradle

apply plugin: 'com.android.application' 

    repositories { 
     mavenLocal() 
     flatDir { 
      dirs 'libs' 
     } 
    } 
    android { 
     compileSdkVersion 25 
     buildToolsVersion "25.0.2" 

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


    dependencies { 
     compile fileTree(dir: 'libs', include: ['*.jar']) 
     testCompile 'junit:junit:4.12' 
     compile 'com.google.android.gms:play-services:10.2.0' 

     compile 'com.android.support:design:25.0.0' 
     compile 'com.github.bumptech.glide:glide:3.6.1' 
     compile 'de.hdodenhof:circleimageview:1.3.0' 
     compile 'com.android.support:appcompat-v7:25.0.0' 

     // Google 
     compile 'com.google.android.gms:play-services-auth:10.2.0' 

     // Firebase 
     compile 'com.google.firebase:firebase-core:10.2.0' 
     compile 'com.google.firebase:firebase-messaging:10.2.0' 
     compile 'com.google.firebase:firebase-database:10.0.0' 
     compile 'com.google.firebase:firebase-auth:10.0.0' 
     compile 'com.google.firebase:firebase-config:10.0.0' 
     compile 'com.google.android.gms:play-services-appinvite:10.2.0' 
     compile 'com.google.android.gms:play-services-ads:10.2.0' 
     compile 'com.google.firebase:firebase-crash:10.0.0' 
     compile 'com.google.firebase:firebase-appindexing:10.0.0' 

     // Payments 
     compile 'com.firebaseui:firebase-ui-database:0.4.0' 
     compile 'com.braintreepayments.api:drop-in:3.+' 
     compile 'com.loopj.android:android-async-http:1.4.9' 
     compile 'com.google.android.gms:play-services-auth:10.2.0' 
     compile 'com.facebook.android:facebook-android-sdk:4.+' 
     compile 'com.google.android.gms:play-services-wallet:10.2.0' 
     compile 'com.firebase:geofire-android:2.1.1' 

    } 

apply plugin: 'com.google.gms.google-services' 

这是推动我们疯了,我不知道为什么它没有收到消息。我们再次从Firebase通知控制台发送消息。我们发送了一条消息给整个应用程序,它仍然没有任何作用。请帮助我们。非常感谢。

+0

你的项目中有'google_service.json'吗? – WenChao

+0

是的,我喜欢。我不知道为什么它不起作用。我正在运行Firebase消息10.0.0。这会有所作为吗?在他们使用10.2.0的文档中? –

+0

我把所有东西都更新到了10.2.0,而且什么都没有。我在应用程序处于前台并且应用程序处于后台时没有发送消息。 –

回答

1

尝试从onCreate 删除Firebase.initializeApp(this)。这是不需要的,可能是问题

+0

有趣的是,我从来没有用Firebase做过这件事,但是当我运行这个活动时。 Firebase引发了这个异常,并告诉我把它放在其他应用程序崩溃。我会看看我是否可以删除它 –

+0

当我删除它时,我无法获取FirebaseInstance,因此无法获取令牌。 Firebase将我抛出此错误并崩溃应用程序。 java.lang.IllegalStateException:java.lang.IllegalStateException:缺省的FirebaseApp在此进程中未初始化com.example.beatrice.mylocalbartender。确保首先调用FirebaseApp.initializeApp(Context)。 –

+0

我从来没有使用过这个电话给我的应用程序。无论如何,您可能会发现在Firebase文档中建议此调用来自应用程序级别,而不是来自Activity。我想可能是这样,被你的方式调用,这可能会重新启动你的应用程序,并驳回你之前的令牌。这只是建议 – atlascoder

1

正如我尚未对此发表评论:

我不是很确定什么是“启用Android的”和“Android的出口”标记的含义,并在舱单服务声明做。无论如何,它适用于我即使没有这些标签。

我的MessagingService看起来有点不同:

构造函数是空的(甚至不叫超(...))。 但我打电话

super.onMessageReceived(remoteMessage); 

在我onMessageReceived方法。

尝试将其添加到您的onMessageReceived方法:)

编辑: 我只用2,需要重写“请勿打扰” - 模式,并启动服务,完成了设备启动时的权限。

我的清单看起来像这样(只是FCM部分):

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

我的服务是这样的:

public class MyAppFirebaseService extends FirebaseMessagingService { 

    public MyAppFirebaseService() { 

    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // ... 
     super.onMessageReceived(remoteMessage); 
     String title = "Title"; 
     String message = "Message"; 


     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      title = remoteMessage.getData().get("title"); 
      message = remoteMessage.getData().get("body"); 
     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      title = remoteMessage.getNotification().getTitle(); 
      message = remoteMessage.getNotification().getBody(); 
     } 
     //My method to display (create) the notification 
     //Basically contains a notification(compat)builder 
     displayNotifcation(title, message); 

    } 
} 

太了解如何通知生成器的工作方式阅读本文档: Android Developer Notification.Builder

+0

我试过了,仍然没有。当我在模拟器上运行它时,它甚至不起作用。有没有我缺少的某种权限? –

+0

等一下,我会将我的代码添加到我的文章 –

+0

我添加了您的内容,但不幸的是,它仍然无法正常工作。另一个奇怪的问题是,我只能从Firebase控制台向令牌发送一条消息。即使令牌没有刷新,当我再次向同一令牌发送消息时,它仍然失败。当我最初向令牌发送消息时,Firebase控制台确实表示它已“完成”,但我显然在我的Android应用程序上看不到任何内容 –

0

发送消息时,您必须使用某个指定频道,并且您的应用程序应该使用sub在此频道上订阅scribeOnTopic( “tome_topic”)。

在例如,对于频道的“新闻”,你将消息发送到“/主题/新闻”频道,你的应用程序必须以接收邮件此通道上进行订阅:

FirebaseMessaging.getInstance().subscribeOnTopic("news"); 

刚将此添加到您的onTokenRefresh()回调或呼叫您想要的地方。

如果您不想接收来自“新闻”频道的消息,请使用 FirebaseMessaging.getInstance()。unsubscribeFromTopic(“news”);

请注意,从应用服务器发送时,您应该为频道名称使用“/ topics/news”,但在您的应用中只使用“新闻”。

关于只有一个onTokenRefresh()调用 - 它确定它不被称为每次重新启动应用程序。 Firebase客户端库在重新启动之间存储令牌,并仅在需要时进行刷新。

+0

我正在尝试向单个用户发送消息。不是那些订阅了主题的人。问题是,当我直接向该令牌发送消息时,Firebase控制台会给我一个未注册的令牌错误。我不明白为什么我会得到这个错误,因为令牌没有被刷新,所以它怎么可以不注册 –

+0

据我所知,令牌是由firebase云生成的。所以我可能假设您可能使用了其他Firebase应用程序的配置文件json,或者您的令牌不存在。您可以随时通过FirebaseInstanceId.getInstance()。getToken()检查您的令牌状态。 – atlascoder

+0

这是我得到令牌的方式,因为onTokenRefresh很少会触发。获得该令牌后,我会转到Firebase通知控制台。输入令牌并发送消息。当我第一次使用令牌时,控制台表示它已“完成”发送消息。但是,Android应用程序没有任何反应。即使令牌没有刷新,并且我再次将消息发送给相同的令牌,Firebase也会抛出“未注册的令牌”错误。我也尝试通过'用户段'向整个应用程序发送消息,但没有任何消息。 –

0

从清单文件中删除工具:node =“replace”。