android解析时间戳,Android / Firebase – 在GCM事件中解析时间戳时...

即使通知显然是受支持的元素(根据Firebase Web文档),我可以摆脱异常的唯一方法是完全删除它,并仅使用数据部分,然后在我的应用程序中创建通知(而不是而不是让firebase做通知).

我的通知现在如下所示:

$fields = array("to" => "",

"data" => array("data"=>

array(

"message"=>"This is some data",

"title"=>"This is the title",

"is_background"=>false,

"payload"=>array("my-data-item"=>"my-data-value"),

"timestamp"=>date('Y-m-d G:i:s')

)

)

);

...

...

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

我的onMessageReceived看起来像这样:

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.

if (remoteMessage.getData().size() > 0) {

Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

try {

JSONObject json = new JSONObject(remoteMessage.getData().toString());

handleDataMessage(json);

} catch (Exception e) {

Log.e(TAG, "Exception: " + e.getMessage());

}

}

}

调用handleDataMessage,如下所示:

private void handleDataMessage(JSONObject json) {

Log.e(TAG, "push json: " + json.toString());

try {

JSONObject data = json.getJSONObject("data");

String title = data.getString("title");

String message = data.getString("message");

boolean isBackground = data.getBoolean("is_background");

String timestamp = data.getString("timestamp");

JSONObject payload = data.getJSONObject("payload");

// play notification sound

NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());

notificationUtils.playNotificationSound();

if (!NotificationUtils.isBackgroundRunning(getApplicationContext())) {

// app is in foreground, broadcast the push message

Intent pushNotification = new Intent(ntcAppManager.PUSH_NOTIFICATION);

pushNotification.putExtra("message", message);

LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

} else {

// app is in background, show the notification in notification tray

Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);

resultIntent.putExtra("message", message);

showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);

}

} catch (JSONException e) {

Log.e(TAG, "Json Exception: " + e.getMessage());

} catch (Exception e) {

Log.e(TAG, "Exception: " + e.getMessage());

}

}

然后调用showNotificationMessage

/**

* Showing notification with text only

*/

private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {

notificationUtils = new NotificationUtils(context);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

notificationUtils.showNotificationMessage(title, message, timeStamp, intent);

}

然后是notificationUtils.showNotificationMessage

public void showNotificationMessage(String title, String message, String timeStamp, Intent intent) {

showNotificationMessage(title, message, timeStamp, intent, null);

}

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {

// Check for empty push message

if (TextUtils.isEmpty(message))

return;

// notification icon

final int icon = R.mipmap.ic_launcher;

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

final PendingIntent resultPendingIntent =

PendingIntent.getActivity(

mContext,

0,

intent,

PendingIntent.FLAG_CANCEL_CURRENT

);

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(

mContext);

final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE

+ "://" + mContext.getPackageName() + "/raw/notification");

showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);

playNotificationSound();

}

private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

inboxStyle.addLine(message);

Notification notification;

notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)

.setAutoCancel(true)

.setContentTitle(title)

.setContentIntent(resultPendingIntent)

.setSound(alarmSound)

.setStyle(inboxStyle)

.setWhen(getTimeMilliSec(timeStamp))

.setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))

.setContentText(message)

.build();

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(ntcAppManager.NOTIFICATION_ID, notification);

}

上面链接中的更多细节,这是很多处理,但至少异常已经消失,我控制通知.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值