自定义通知

吉西什PN

使用本教程实现了自定义通知

问题是现在收到2条通知。一个是默认Notification,另一个是自定义Notification

如何禁用默认通知。

代码 :

 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);
}
确定设置

可能您正在尝试操纵“解析通知”。

所以如果你改变你的 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(),因此您只会看到自定义的推送通知

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章