使用此tutorial
实施自定义通知问题现在收到2个通知。 一个是默认通知,另一个是自定义通知。
如何停用默认通知。
代码:
public void showNotificationMessages(String title, String message, Intent intent) {
int icon = R.mipmap.ic_launcher;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(icon)
.setContentTitle("Project")
.setContentText(""+title+" custom notification")
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) mContext. getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
清单
<receiver android:name=".receiver.CustomPushReceiver"
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" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
截图
CustomReceiver
@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());
}
}
private void parsePushJson(Context context, JSONObject json) {
try {
String title = json.getString("alert");
Intent resultIntent = new Intent(context, MainActivity.class);
showNotificationMessage(context, title, "", resultIntent);
} catch (JSONException e) {
Log.e(TAG, "Push message json exception: " + e.getMessage());
}
}
private void showNotificationMessage(Context context, String title, String message, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessages(title, message, intent);
}
答案 0 :(得分:1)
可能您正试图操纵您的Parse Notifications。
因此,如果您更改了onPushReceive
@Override
protected void onPushReceive(Context context, Intent intent) {
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());
}
return;
}
删除super.OnPushReceive()
后,您将无法通过解析获取推送通知,因此您只会看到自定义推送通知
答案 1 :(得分:0)
如果您共享CustomReceiver会很棒,但根据我的经验,您应该扩展GcmReceiver
或任何其他可能已经扩展它的类。
答案 2 :(得分:0)
以下代码将帮助您在所有Android设备上显示自定义通知。
private static void showNotification(Context context, int notificationId, Uri sound) {
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Intent starterIntent = new Intent(context, SplashActivity.class);
final PendingIntent pi = PendingIntent.getActivity(context, 0, starterIntent, 0);
final String CHANNEL_ID = "UniqueChannelId"; // The id of the channel.
CharSequence name = context.getString(R.string.app_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel androidChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
androidChannel.enableLights(true);
// Sets whether notification posted to this channel should vibrate.
androidChannel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
androidChannel.setLightColor(Color.BLUE);
// Sets whether notifications posted to this channel appear on the lockscreen or not
androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(androidChannel);
}
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentText(name)
.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_anom)
.setContentIntent(pi)
.setAutoCancel(true);
NotificationCompat.Style style = new NotificationCompat.InboxStyle();
notification.setStyle(style);
notification.setSound(sound, AudioManager.STREAM_NOTIFICATION);
notification.setLights(Color.BLUE, 5000, 5000);
notification.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
Notification build = notification.build();
if (notificationManager != null) {
notificationManager.notify(notificationId, notification.build());
}
}