FCM/GCM推送带图片
2018-12-02 本文已影响0人
沫晚霞光
1.问题背景
公司项目要求到达手机的推送通知里能够放上一张背景图, 以及自定义通知消息, 使得推送看起来炫酷一些
下面这个是一条手机通知
-----------------------------------------------------------------
| |图标| [这是标题] [背景图片占满整个通知栏]
| |区域| [这是标题内容]
-----------------------------------------------------------------
2.思路
2.1 在消息字段里添加一条图片链接, 当消息到达时去请求这个图片链接
2.2 Android自定义通知栏条目显示, 需要
(1)编辑自定义的layout文件(xxxx.xml), 要包含一个ImageView控件, 用于放置我们加载的图片
(2)用RemoteView修改默认通知栏条目显示, 使用自定义的layout文件
3.开搞
3.1 如果不熟悉FCM, 传送门
3.2 在继承FirebaseMessagingService的类(一个你自己的命名)里
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
// FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
try {
// inspect your own notification data
Log.d(SIG, "From: " + remoteMessage.getFrom());
Log.d(SIG, "data: " + remoteMessage.getData());
if (remoteMessage.getData() != null) {
Log.d(SIG, "title: " + remoteMessage.getData().get("title"));
Log.d(SIG, "content data: " + remoteMessage.getData().get("content"));
Log.d(SIG, "messageCode" + remoteMessage.getData().get("messageCode"));
}
if (remoteMessage.getNotification() != null) {
Log.d(SIG, "title: " + remoteMessage.getNotification().getTitle());
Log.d(SIG, "data: " + remoteMessage.getNotification().getBody());
}
} catch (Exception e) {
Log.e(SIG, e.getMessage(), e);
}
if (context == null) {
context = Cocos2dxApp.getContext();
if (context == null) {
// when context equals null, means app closed, use service context,
// however, intent should be changed
context = this;
}
}
notifyCustomMessage(remoteMessage);
}
private void notifyCustomMessage(RemoteMessage message)
{
// title
String titleString = "";
if (message.getData() != null) {
titleString = message.getData().get("title");
} else if (message.getNotification() != null) {
titleString = message.getNotification().getTitle();
} else {
return;
}
// content
String contentTextString = "";
if (message.getData() != null) {
contentTextString = message.getData().get("content");
} else if (message.getNotification() != null) {
contentTextString = message.getNotification().getBody();
} else {
return;
}
// bgImageUrlField
String bgImageUrlField = null;
if (message.getData() != null) {
bgImageUrlField = message.getData().get("bgImageUrl");
}
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// an intent when notification bar is clicked
// Replace [SplashActivity.class] using your own app startup activity
Intent it = new Intent(context, SplashActivity.class);
it.putExtra("messageCode", message.getData().get("messageCode"));
PendingIntent contentIndent = PendingIntent.getActivity(context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.push_notify_view);
// notification's icon
remoteViews.setImageViewResource(R.id.notif_icon, R.mipmap.ic_launcher);
// notification's title
remoteViews.setTextViewText(R.id.notif_title, titleString);
// notification's content
remoteViews.setTextViewText(R.id.notif_content, contentTextString);
// notification's time
Log.d(SIG, "test time: " + ( new SimpleDateFormat("HH:mm").format(new Date())) );
remoteViews.setTextViewText(R.id.notif_cur_time, new SimpleDateFormat("HH:mm").format(new Date()));
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(titleString)
.setTicker(titleString)
.setContentText(contentTextString)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setWhen(System.currentTimeMillis())
.setContent(remoteViews)
.setAutoCancel(true)
.setContentIntent(contentIndent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build();
// notification's background
final String bgImageUrl = bgImageUrlField;
final RemoteViews rmv = remoteViews;
final NotificationManager nfm = notificationManager;
final Notification nf = notification;
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable(){
@Override
public void run() {
if ( ! bgImageUrl.equals(null)) {
Picasso.with(getApplicationContext()).load(bgImageUrl).into(rmv, R.id.push_bg, status_code, nf);
}
nfm.notify(status_code, nf);
status_code++;
}
});
}
3.3 编辑自己的layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="@+id/notif_small">
<ImageView
android:id="@+id/push_bg"
android:contentDescription=""
android:layout_width= "1136dp"
android:layout_height="105dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:scaleType="centerCrop"/>
<ImageView
android:id="@+id/notif_icon"
android:contentDescription=""
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="@mipmap/ic_launcher"
android:layout_marginLeft="7dp"
android:layout_marginRight="9dp"/>
<TextView
android:id="@+id/notif_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:singleLine="true"
android:paddingTop="18dp"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="@string/app_name"/>
<TextView
android:id="@+id/notif_cur_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="22dp"
android:paddingRight="30dp"
android:singleLine="true"
android:textSize="16sp"
android:textColor="#ffffff"
android:text="Content" />
<TextView
android:id="@+id/notif_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notif_icon"
android:paddingBottom="19dp"
android:layout_alignParentBottom="true"
android:singleLine="true"
android:textSize="20sp"
android:textColor="#FFFF70"
android:text="Content" />
</RelativeLayout>
3.4 在自己的build.gradle里添加Picasso支持
dependencies {
//...
// Picasso Image Library
compile 'com.squareup.picasso:picasso:2.5.2'
// ...
}
4. 注意事项
(1) Google FCM默认有两种消息:(1)数据消息 (2) 通知消息
要使用自定义通知形式, 必须使用通知消息
相关参考:
传送门1
传送门2
(2) 目前使用Google的Firebase控制台只能发送通知消息
虽然代码很糟糕, 但亲测可用!
虽然代码很糟糕, 但亲测可用!
虽然代码很糟糕, 但亲测可用!