【问题标题】:How to style push notification by JSON with parse.com [Android]如何使用 parse.com [Android] 通过 JSON 设置推送通知样式
【发布时间】:2014-11-07 04:33:30
【问题描述】:

我想知道如何使用 parse.com 服务在 android 手机上设置推送通知的样式。 哪些字段可用,是否有可能设置推送通知的颜色、图像和格式 [粗体、斜体、...]? 谢谢

【问题讨论】:

  • 只需学习如何在 android 中为通知设置样式,因为从 parse.com 推送到您的设备的任何内容都将根据您的代码(用于通知)以样式显示给用户android 应用,但不是 parse.com 推送通知代码。参考this tutorial 了解如何处理 android 通知。

标签: android push-notification parse-platform push


【解决方案1】:

对于 Android 推送通知,您可以自定义以下内容:

  1. alert:通知消息。
  2. action: Intent 应该在收到推送时触发。如果没有指定标题或警报值,则 Intent 将被触发,但不会向用户显示任何通知。
  3. title:在 Android 系统托盘或 Windows 8 Toast 通知中显示的值。

Source

这个tutorial 可能会有用。

【讨论】:

  • 可以改变文本的格式吗?!例如标题使用斜体样式等等?
  • 我认为这是不可能的。对不起。
【解决方案2】:

您可以开发自己的风格 第一的 : 在清单中添加自定义推送接收器 Like blew :

<receiver android:name=".CustomPushReceiver" android:exported="false">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
    <action android:name="android.intent.action.USER_PRESENT"/>
    <action android:name="com.parse.push.intent.RECEIVE"/>
    <action android:name="com.parse.push.intent.DELETE"/>
    <action android:name="com.parse.push.intent.OPEN"/>
</intent-filter>

自定义推送接收器:

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.parse.ParsePushBroadcastReceiver;

import mainViews.MainPage;

import org.json.JSONException;
import org.json.JSONObject;



/**
 * Created by SadSad on 01/06/15.
 */
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);

        System.out.println(intent.getExtras().getString("com.parse.Data"));

        if (intent == null)
            return;

        try {
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            System.out.println(json );

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

    /**
     * Parses the push notification json
     *
     * @param context
     * @param json
     */
    private void parsePushJson(Context context, JSONObject json) {
        try {
            System.out.println(json);
            boolean isBackground = json.getBoolean("is_background");
            JSONObject data = json.getJSONObject("data");
            String title = data.getString("title");
            String message = data.getString("message");

            if (!isBackground) {
                Intent resultIntent = new Intent(context, MainPage.class);
                showNotificationMessage(context, title, 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) {

    // you can use custom notification 
        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);
    }
}

自定义通知: This link is useful for custom notification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多