0

我在項目中使用了Firebase身份驗證材料。今天我決定將FCM整合到我的項目中。但我無法從我的Firebase控制檯收到任何通知。我也嘗試重新下載.json文件,但沒有成功。你們對我的問題有什麼想法嗎?我的設備未收到Firebase雲消息傳遞通知

的AndroidManifest.xml

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

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

<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<!-- Cihazdan Google Servislerine erişmek için izin --> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<!-- İndirilenlerin tutulması için izinler --> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<!-- OpenGL ES 2.0 Google Maps Android API v2 için gerekiyor --> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"></uses-permission> 
<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/oto_takip" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MyIntro" 
     android:theme="@style/FullscreenTheme" /> 
    <activity 
     android:name=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".AracEkle" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".InternetCon" 
     android:theme="@style/FullscreenTheme" /> 
    <activity 
     android:name=".IpuclariSayfasi" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".TrafficActivity" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".EngineActivity" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".AidActivity" 
     android:theme="@style/AppTheme.NoActionBar" /> 

    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps" /> 

    <activity 
     android:name=".AyarlarActivity" 
     android:theme="@style/AppTheme.NoActionBar"> 
    </activity> 

    <activity android:name=".Hesabim" 
     android:theme="@style/AppTheme.NoActionBar"> 
    </activity> 

    <activity android:name=".Giris" 
     android:theme="@style/AppTheme.NoActionBar" 
     > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> <!-- Check this one --> 
     </intent-filter> 
    </activity> 

    <activity android:name=".KayitOl" 
     android:theme="@style/AppTheme.NoActionBar"> 
    </activity> 

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

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

</application> 

FirebaseMessageService.java

public void onMessageReceived(RemoteMessage remoteMessage){ 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 

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, intent, PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.oto_takip) 
      .setContentText(messageBody) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, notificationBuilder.build()); 
} 

FirebaseIDService.java

public class FirebaseIDService extends FirebaseInstanceIdService 
{ 
private static final String TAG ="FirebaseIDService"; 
@Override 
public void onTokenRefresh() { 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.d(TAG, "Token: " + refreshedToken); 

    // If you want to send messages to this application instance or 
    // manage this apps subscriptions on the server side, send the 
    // Instance ID token to your app server. 
    sendRegistrationToServer(refreshedToken); 
} 

private void sendRegistrationToServer(String refreshedToken) 
{ 
} 
    } 

我認爲這個問題不是由我的MainActiviy.java決定的,但我會向你展示以防萬一。

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener{ 

private Fragment fragment; 
private FragmentManager fragmentManager; 
public TextView tarih; 
final String PREFS_NAME = "MyPrefsFile"; 
final String SHAREDPREF_DATE = "SharedPrefDate"; 
private FirebaseAuth firebaseAuth; 
private TextView textViewUserEmail; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    firebaseAuth = FirebaseAuth.getInstance(); 
    if(firebaseAuth.getCurrentUser()==null) 
    { 
     startActivity(new Intent(this,Giris.class)); 
     finish(); 
    } 
    setContentView(R.layout.activity_main); 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Navigation itemlerinin click olaylarını burada yapıyoruz. 
    int id = item.getItemId(); 
    if (id == R.id.nav_kullanici) 
    { 
     startActivity(new Intent(MainActivity.this,Hesabim.class)); 

    } 
    else if (id == R.id.nav_hatirlatma) 
    { 
     startActivity(new Intent(MainActivity.this,AyarlarActivity.class)); 
    } 
    else if (id == R.id.nav_ipucu) 
    { 
     startActivity(new Intent(MainActivity.this,IpuclariSayfasi.class)); 
    } 


    else if (id == R.id.nav_oyla) 
    { 


    } 
    else if (id == R.id.nav_cikis) 
    { 

     firebaseAuth.signOut(); 
     finish(); 
     startActivity(new Intent(MainActivity.this, Giris.class)); 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 

} 
} 
+0

您是否嘗試直接發送到FCM令牌? –

+0

不,我該怎麼做?但我沒有發送消息。我想收到消息。 –

+0

順便說一句,我想在Android模擬器上接收消息。這個可以嗎? –

回答

0

您可能要檢查是否已正確地配置您的應用程序中使用火力地堡。在項目的應用程序目錄中的build.gradle文件中添加google-services插件。

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

查看此tutorial瞭解更多信息。

除此之外,add FCM dependency。確認您的app/build.gradle文件中是否存在此依賴關係。

compile 'com.google.firebase:firebase-messaging:10.2.0' 

SO post的建議解決方案也可能有所幫助。

相關問題