'Android: How to notification to notification

I am super beginner. Now I am try to make Android notification using NotificationListenerService.

Problem:

[A]application don't post notification but I can get to check (A)application debuglog in logcat .

I want to automatically post notification as [B]application when [A] post notification .

(I am making [B]application. )

Now:

I can get [A]application package name when it post. And then ...stop , how can I post notification(id & package name) from [B]application? use broadcastreceiver?

NotificationService.java

package com.testnotification;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;


public class NotificationService extends NotificationListenerService {
    private String TAG = "Notification";
    private String Sample = "com.notification.sample";    ///this is [A]application

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        String packagename = sbn.getPackageName();

        if(packagename.equals(Sample)){
            showLog(sbn);
        }
    }

    private void showLog( StatusBarNotification sbn ){
        int id = sbn.getId();
        String name = sbn.getPackageName();
        Log.d(TAG,"id:" + id +
                " name:" + name);
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testnotification">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Testnotification">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".NotificationService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
            android:exported="true">
        <intent-filter>
            <action android:name=
                "android.service.notification.NotificationListenerService" />
        </intent-filter>
        </service>

    </application>

</manifest>

MainActivity.java(empty yet)

Please teach me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source