如何在推送通知项目中修改固定标题?

时间:2016-12-15 08:30:46

标签: android firebase push-notification firebase-cloud-messaging

我正在尝试修改我的项目,因此我可以发送带有标题和消息的推送通知,我只能从我的网络服务器和标题Elt发送通知消息,如下面的代码所示是固定的。

Firebasemessagingservice.java

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getData().get("message"));
}



private void showNotification(String message) {

    Intent i = new Intent(this,MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Elt")
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            //.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());

1 个答案:

答案 0 :(得分:0)

我有个主意。当您发送消息时,只需将第一个WORD作为标题,然后将其拆分。

//example message= "MYTITLE This is my message";
String arr[] = message.split(" ", 2);
String title= arr[0];   //MYTITLE 
String message1 = arr[1];     //This is my message

请参阅下面的完整代码:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getData().get("message"));
}



private void showNotification(String message) {

     //example message= "MYTITLE This is my message";
    String arr[] = message.split(" ", 2);
    String title= arr[0];   //MYTITLE 
    String message1 = arr[1];     //This is my message

    Intent i = new Intent(this,MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(title)  //here the title
            .setContentText(message1)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            //.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());