我正在通过Parse Server实现Firebase推送通知。
当我通过仪表板发送通知时,过去将其显示为已发送。
我无法接收/不接收推送通知
AndroidManifeast.xml
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_trot_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />
<meta-data
android:name="com.parse.SERVER_URL"
android:value="@string/parse_server_url" />
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="@string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="@string/back4app_client_key" />
<meta-data android:name="com.parse.push.gcm_sender_id"
android:value="94570192751" />
build.gradle
implementation "com.github.parse-community.Parse-SDK-Android:parse:1.22.1"
implementation "com.github.parse-community.Parse-SDK-Android:fcm:1.22.1"
我正在MainActivity.java中添加数据
ArrayList<String> channels = new ArrayList<>();
channels.add(ParseConstants.NOTIFICATION_CHANNEL_PLACES);
channels.add(ParseConstants.NOTIFICATION_CHANNEL_FRIENDS);
channels.add(ParseConstants.NOTIFICATION_CHANNEL_SYSTEM);
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put(ParseConstants.GCM_SENDER_ID, getString(R.string.gcm_sender_id));
installation.put(ParseConstants.CHANNEL, channels);
installation.put(ParseConstants.USERNAME,ParseUser.getCurrentUser().getUsername());
installation.saveInBackground();
我已删除以下依赖项
实现'com.google.firebase:firebase-analytics:17.0.1'实施'com.google.firebase:firebase-messaging:20.1.0'实施'com.google.firebase:firebase-core:17.2.1'
已添加
实现'com.google.android.gms:play-services-gcm:17.0.0'
在Androidmanifest.xml
中添加
在application
标签中
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<!-- for Gingerbread GSF backward compat -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="packagename" />
</intent-filter>
</receiver>
<service
android:name=".utils.PushNotificationReceiver" android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
PushNotificationReceiver
扩展了GcmListenerService
覆盖onMessageReceived
@Override
public void onMessageReceived(String from, Bundle data) {
try {
if (data.containsKey("data")) {
JSONObject jsonObject = new JSONObject(data.getString("data"));
sendNotification(jsonObject.getString("title"), jsonObject.getString("message"),
jsonObject.getString("channel"));
}
} catch (Exception ex) {
}
}
private void sendNotification(String title, String message, String channel) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channel,
channel, NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
Intent intent = new Intent(this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channel);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.trot_icon_with_space)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setContentInfo("Info");
if (notificationManager != null) {
int pushNotificatonId = new Random().nextInt(10000);
notificationManager.notify(/*notification id*/pushNotificatonId, notificationBuilder.build());
}
}