从通知Payload中删除3个点

时间:2017-05-26 13:23:46

标签: android push-notification firebase-notifications

我做了什么:

我已经完成了通知部分,它在通知托盘中显示多个通知而不覆盖它。

我想要实现的目标:

我想删除在通知托盘的数据有效负载中的第7个通知之后显示的三个点(...)。就像在第二张图片中一样,在WhatsApp中第7次通知后没有连续点。我做了很多研究,但没有找到任何可行的解决方案。

图片1

enter image description here

图片2

enter image description here

以下是我的代码:

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fcmdemo">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon_btn_volume"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <receiver android:name="NotificationActionReceiver">
            <intent-filter>
                <action android:name="CONFIRM" />
                <action android:name="CANCEL" />
            </intent-filter>
        </receiver>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/icon_btn_volume" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </application>

</manifest> 

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
    public static ArrayList<String> notifications = new ArrayList<>();
    private String remoteMSG;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Log data to Log Cat
        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        remoteMSG = remoteMessage.getNotification().getBody();
        notifications.add(remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());

    }

    private void sendNotification(String messageBody) {
        int i;
        //onDismiss Intent
        Intent intent = new Intent(this, NotificationActionReceiver.class);
        PendingIntent broadcastIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);

        //OnClick Listener
        startWFApplication().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, startWFApplication(),
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_btn_volume)
                .setContentTitle("Title")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        // Sets a title for the Inbox in expanded layout
        inboxStyle.setBigContentTitle("Title - Notification");
        inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");

        // Moves events into the expanded layout
        //notifications.add(messageBody);
        for (i = 0; i < notifications.size(); i++) {
            inboxStyle.addLine(notifications.get(i));
        }


        // Moves the expanded layout object into the notification object.
        notificationBuilder.setStyle(inboxStyle);

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

        notificationManager.notify(0, notificationBuilder.build());
        notificationBuilder.setDeleteIntent(broadcastIntent);
    }

    public static Intent startWFApplication() {
        Intent launchIntent = new Intent();
        launchIntent.setComponent(new ComponentName("com.fcmdemo", "com.fcmdemo.MyFirebaseMessagingService"));
        return launchIntent;
    }

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);
        Log.e(TAG, "handleIntent" + remoteMSG);
    }
}

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。我只是在添加之前加上条件。

for (i = 0; i < notifications.size(); i++) {
            if (i <= 6) {
                inboxStyle.addLine(notifications.get(i));
                inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");
            } else {
                inboxStyle.setSummaryText("You have " + notifications.size() + " Notifications.");
            }
 }