自定义通知

custom notification

使用此 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>

截图

自定义接收器

@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);
}

可能您正试图操纵您的 Parse 通知。

因此,如果您更改 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,或者任何其他可能已经扩展它的 class。

以下代码将帮助您在所有 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());
        }
    }