Android 应用程序未收到任何Firebase云消息
我正在尝试使用firebase云消息打开某些活动。我正试图将以下JSON作为通知发送到openAndroid 应用程序未收到任何Firebase云消息,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我正在尝试使用firebase云消息打开某些活动。我正试图将以下JSON作为通知发送到openDAILYDOSEactivity { "to": "/topics/DAILY_DOSE" , "data": { "extra_information": "This is some extra information" }, "notification": { "title": "NEW NOTIFICATION!", "text": "Cli
DAILYDOSE
activity
{
"to":
"/topics/DAILY_DOSE"
,
"data": {
"extra_information": "This is some extra information"
},
"notification": {
"title": "NEW NOTIFICATION!",
"text": "Click me to open an Activity!",
"click_action": "DAILYDOSE"
}
}
但我不确定为什么我的应用程序中没有收到任何通知。
以下是我处理通知的FirebaseMessagingService
类:
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingService";
public FirebaseMessagingService() {}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.d(TAG, "Message Notification click_action: " + click_action);
sendNotification(title, message, click_action);
}
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title, String messageBody, String click_action) {
Intent intent;
if (click_action.equals("DAILYDOSE")) {
intent = new Intent(this, DailyDose.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(this, ProfileActivity.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.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build());
}
}
我已在我的档案活动
活动中定义了主题
:
public class ProfileActivity extends AppCompatActivity {
private TextView userName, emailAddress, emailVerified;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mAuth = FirebaseAuth.getInstance();
FirebaseMessaging.getInstance().subscribeToTopic("DAILY_DOSE");
}
}
这是我的舱单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUpActivity" />
<activity android:name=".ProfileActivity" />
<activity android:name=".DailyDose">
<intent-filter>
<action android:name="DAILYDOSE"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
我不确定为什么我没有收到任何通知。如果您在Oreo或更高版本上测试,请尝试使用通知频道。我使用的是android版本p@mnp343Pie高于Oreo。因此,您需要实现通知通道。