Android 自定义通知
使用以下命令实现了自定义通知 问题现在收到2个通知。 一个是默认通知,另一个是自定义通知 如何禁用默认通知。 代码:Android 自定义通知,android,push-notification,Android,Push Notification,使用以下命令实现了自定义通知 问题现在收到2个通知。 一个是默认通知,另一个是自定义通知 如何禁用默认通知。 代码: public void showNotificationMessages(String title, String message, Intent intent) { int icon = R.mipmap.ic_launcher; PendingIntent resultPendingIntent = Pendi
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>
可能您正试图操纵解析通知 因此,如果您更改您的
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()
,您将无法从解析中获取推送通知,因此您将只能看到自定义推送通知如果您与他人分享您的CustomReceiver,那将非常好,但根据我的经验,您应该扩展GcmReceiver
,或者任何其他可能已经扩展它的类。以下代码将帮助您在所有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());
}
}
你能分享你的
.receiver.CustomPushReceiver
吗?现在分享这个方法codeparsePushJson
。。共享在何处生成通知的完整代码。请检查我编辑的答案@JPn
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());
}
}