kaz*_*ama 5 android push-notification android-notifications firebase firebase-notifications
我正在尝试从 Firebase 控制台发送推送通知,目前我可以从 Firebase 控制台向我的虚拟设备发送消息,但如果消息很长,则不会完全显示在通知栏中。
这是 Firebasemessagingservice 的代码:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
/**
* Created by filipp on 5/23/2016.
*/
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("Elit")
.setContentText(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());
}
}
Run Code Online (Sandbox Code Playgroud)
感谢 Adib 的详细回答,我已经有一个 PHP 后端服务器,但是每当我尝试从服务器发送长通知时,它都不会完全显示,我的问题是我是否可以只编辑代码的最后一部分,以便我可以发送长通知使用 inboxStyle 或 bigtextstyle 来自我的服务器的通知。
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("Elit")
.setContentText(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());
}
Run Code Online (Sandbox Code Playgroud)
参考此链接,您仍然无法通过 Firebase 以大通知样式发送推送通知。除非 Firebase 库添加此功能并开始支持它,否则它将始终采用简单的单一通知样式。
但是,您可以调整它以按照您的方式工作。但需要注意的是,如果您通过 Firebase 项目控制台的“通知”部分发送数据,则无法执行此过程。仅当您的应用程序有后端并且推送通知通过网络发送时才能完成此操作。
现在我们已经确保所有推送数据都直接到达FirebaseMessagingService 类中的onMessageReceived(RemoteMessage RemoteMessage)方法,而无需自行创建通知,您需要根据收到的数据创建通知。下面是用于实现该目的的示例代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, whatToOpen, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setAutoCancel(true)
.setSmallIcon(R.drawable.push_icon_small)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.push_icon_large))
.setSound(defaultSoundUri)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)这部分是让通知变大的原因。
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
Run Code Online (Sandbox Code Playgroud)
现在您甚至可以在推送通知中发送文章!希望这可以帮助!
归档时间: |
|
查看次数: |
4902 次 |
最近记录: |