firebase - 解析服务器中的推送通知
问题描述
我正在通过 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();
解决方案
1.我已经删除了以下依赖项
实施 'com.google.firebase:firebase-analytics:17.0.1'
实施 'com.google.firebase:firebase-messaging:20.1.0'
实施 'com.google.firebase:firebase-core:17.2.1'
2.新增
实施 'com.google.android.gms:play-services-gcm:17.0.0'
3.在Androidmanifest.xml
添加
<permission
android:name="packagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="packagename.permission.C2D_MESSAGE" />
在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>
4.创建新类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());
}
}
推荐阅读
- php - 查找两个已知坐标之间的坐标
- python - AttributeError: module 'tensorflow.io' has no attribute 'experimental'
- pandas - 存储从数据框获得的值
- reactjs - 无法从 Three.js 导入后处理
- python-3.x - Cloud Run - 请求延迟
- flutter - 如何在单击子小部件中存在的按钮时更改父小部件的动画?
- sql - Presto 查询父子层次结构
- for-loop - 循环语法的 mariadb 过程
- sockets - TCP接收队列和发送队列有限制吗?
- python - 用 matplotlib 和 numpy 将两个图相交