解析服务器中的推送通知
Posted
技术标签:
【中文标题】解析服务器中的推送通知【英文标题】:Push notification in parse Server 【发布时间】:2020-01-08 13:43:00 【问题描述】:我正在通过 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();
【问题讨论】:
尝试根据this指南更新您的清单文件并告诉我。 我收到GCM: broadcast intent callback: result=CANCELLED
然后找到解决方案,实现 GCM 接收器。
【参考方案1】:
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());
【讨论】:
以上是关于解析服务器中的推送通知的主要内容,如果未能解决你的问题,请参考以下文章