最佳答案
我有一个与 FireBase 云消息的问题,其中我得到的令牌从设备和发送通知测试通过谷歌 FireBase 通知控制台,但是,通知从来没有记录或推送到安卓虚拟设备。FCM 的文档几乎完全就是我在下面提到的代码,除此之外没有什么别的东西可以让你使用 firebase 来获得推送通知。我已经通过了所有的设置信息(build.gradle 添加,安装谷歌播放服务,等等..。.)但仍然没有生成消息。我确实收到一个一次性的错误后立即推送通知说: “ I/FA: 标签管理器没有找到,因此将不会使用”,但这是唯一的数据输出,我没有发现任何有关标签管理器的要求和 FCM 在谷歌。我没有收到日志猫或设备的推送通知的代码出了什么问题?请让我知道任何进一步的信息,将有助于。 谢谢。
通知 Genie.java
public class NotificationGenie extends FirebaseMessagingService {
private static final String TAG = "Firebase_MSG";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// 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.
sendNotification(remoteMessage.getNotification().getBody());
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, PostLoginActivity.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 = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.tf2spyprofile)
.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());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.test.movile_android">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DashBoardActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".NewOrderActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".PostLoginActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
<service
android:name=".NotificationGenie">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>