博观而约取,厚积而薄发。这篇文章主要讲述推送通知未在Android 8.0中使用解析sdk显示相关的知识,希望能为你提供帮助。
我正在开发android应用程序,我在自定义Parse推送通知类中注册了FCM推送通知。从Parse Dashboard发送Push时,它会进入我的Receiver,但不会显示在我的屏幕的Notification Bar中。我的自定义类以及清单和代码如下所示。我也搜索了许多类似的问题,但没有运气。
我的清单:
<
receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<
intent-filter>
<
action android:name="com.google.android.c2dm.intent.RECEIVE" />
<
action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<
category android:name="com.kolbeh" />
<
/intent-filter>
<
/receiver>
<
meta-data android:name="com.parse.push.gcm_sender_id"
android:value="https://www.songbingjia.com/android/id:854901#####" />
<
service android:name="com.parse.PushService" />
<
receiver
android:name="dinewhere.fcm.CustomPushReceiver"
android:exported="false">
<
intent-filter>
<
action android:name="com.parse.push.intent.RECEIVE" />
<
action android:name="com.parse.push.intent.OPEN" />
<
action android:name="com.parse.push.intent.DELETE" />
<
/intent-filter>
<
/receiver>
我的自定义接收器:
public class CustomPushReceiver extends ParsePushBroadcastReceiver {
private final String TAG = CustomPushReceiver.class.getSimpleName();
private NotificationUtils notificationUtils;
private Intent parseIntent;
public CustomPushReceiver() {
super();
}@Override
protected void onPushReceive(Context context, Intent intent) {
//super.onPushReceive(context, intent);
if (intent == null)
return;
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.e(TAG, "Push received: " + json);
parseIntent = intent;
parsePushJson(context, json);
} catch (JSONException e) {
Log.e(TAG, "Push message json exception: " + e.getMessage());
}
}@Override
protected void onPushDismiss(Context context, Intent intent) {
super.onPushDismiss(context, intent);
}@Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);
System.out.println("asdf");
}/**
* Parses the push notification json
*
* @param context
* @param json
*/
private void parsePushJson(Context context, JSONObject json) {
try {
String message = json.getString("alert");
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra("Chat", true);
showNotificationMessage(context, "Hello", message, resultIntent);
} catch (JSONException e) {
Log.e(TAG, "Push message json exception: " + e.getMessage());
}
}/**
* Shows the notification message in the notification bar
* If the app is in background, launches the app
*
* @param context
* @param title
* @param message
* @param intent
*/
private void showNotificationMessage(Context context, String title, String message, Intent intent) {notificationUtils = new NotificationUtils(context);
intent.putExtras(parseIntent.getExtras());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, intent);
}
}
我的节目通知消息方法:
public void showNotificationMessage(String title, String message, Intent intent) {// Check for empty push message
if (TextUtils.isEmpty(message))
return;
if (airportAssistUtil.isAppIsInBackground(mContext)) {
// notification icon
int icon = R.mipmap.ic_launcher_round;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
Notification notification = mBuilder.setSmallIcon(icon).setTicker("Kolbeh").setWhen(0)
.setAutoCancel(true)
.setStyle(inboxStyle)
.setContentTitle("Kolbeh")
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
mBuilder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count++, notification);
}
}
答案【推送通知未在Android 8.0中使用解析sdk显示】如果您的应用定位到Android O.那么您必须显示带通知频道的通知。来自android O每个通知必须包含在渠道Read here内。
推荐阅读
- 在android studio上设置bitnami Parse服务器的电子邮件验证
- Android片段和依赖注入
- 如何注入动态创建的用例(android,clean architecture,dagger2)
- 如何在Ninject中使用AutoMApper.5.2.0()
- 在Mapping时向Automapper提供构造函数参数
- 如何将Applescript scpt文件转换为命令
- .Net Core App - 命令行格式异常
- 在Android中登录尝试失败时app崩溃了
- 在Android studio中使用活动模板时,我无法添加新的Java类吗()