当前位置: 首页 > 知识库问答 >
问题:

我的设备未收到 Firebase 云消息通知

微生新翰
2023-03-14

我在项目中使用Firebase身份验证。今天我决定将FCM集成到我的项目中。但是我无法从我的Firebase控制台收到任何通知。我还尝试重新下载. json文件,但没有成功。你们知道我的问题吗?

Android清单.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决定的,但以防万一,我还是给你看一下。

主要活动.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;

}
}

共有1个答案

巫马善
2023-03-14

您可能需要检查是否已正确配置应用程序以使用Firebase。在<code>构建中添加谷歌服务插件。项目的app目录中的gradle文件。

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

有关详细信息,请参阅本教程。

除此之外,添加FCM依赖项。确认您的应用程序/构建中是否存在此依赖项。gradle文件。

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

这篇SO文章中建议的解决方案也可能有所帮助。

 类似资料:
  • 我对FireBase云消息有一个问题,我从设备中获取令牌,并通过Google FireBase通知控制台发送通知测试。但是,通知从来没有被记录,也没有被推送到android虚拟设备。FCM的文档几乎与下面的代码完全相同,其他代码很少,您还需要做什么来获得使用FireBase的推送通知。我已经完成了所有的设置信息(build.gradle添加、安装google play服务等)如文档中所指定的,但仍

  • null 当我确实从Firebase云消息发送给自己一条测试消息时,它确实工作正常,并且成功地向每个设备发送了一个推送通知。 例如,以下是 null

  • 我正在接收令牌,但当我从Firebase控制台发送通知时,我的设备上不会发生任何事情,无论应用是否处于后地。与此同时,我的Android应用程序收到通知。 我遵循了这个指南: https://firebase.google.com/docs/cloud-messaging/ios/client 为了确保这一点,我还在 https://github.com/firebase/quickstart-i

  • null 我遵循Xamarin的文档实现了这个功能。 然后一步一步地执行,直到下面的部分: 后台通知 我点击了Log Token按钮并收到了令牌。 null Firebase控制台显示消息为“完成”。 我错过了什么来解决这个问题?

  • 我正在尝试使用Firebase Notification Composer向我的设备发送测试通知。我正在接收FCM令牌并将其打印到我的控制台,然后尝试向该令牌发送通知。 以下是我检查过的内容: 1) 我使用的是iOS 12.4.1 2) 我正在分配消息传递代理 3)我从委托方法接收FCM令牌 4) 我已经验证了通知是通过打印到控制台启用的,当提示我允许通知时,我单击了允许 5) 我已经验证了在Fi

  • 我不明白如何从iOS设备向另一个iOS设备发送消息,我试图理解Firebase通知和Google云消息之间的区别。 null Google Cloud Messaging:它将消息从服务器发送到设备(下游)或设备发送到服务器(上游)!! 上游示例: 如果我需要从一个设备发送一个推送消息到另一个设备呢!这是否意味着在设备发送消息到服务器之后,我必须编程firebase服务器发送推送到客户端?真让人摸