简体   繁体   中英

Android can't show notification

I'm using the NotificationService and a Broadcastreceiver to get the incoming notifications. The service starts from the manifest:

<service android:name="com.myapp.notifications.MyNotificationListenerService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

            <intent-filter>

                <action android:name="android.service.notification.NotificationListenerService" />

            </intent-filter>

        </service>

and it's this;

public class MyNotificationListenerService extends NotificationListenerService{
    Context context;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {


        String pack = sbn.getPackageName();
        String ticker = sbn.getNotification().tickerText.toString();
        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();

        Log.i("Package",pack);
        Log.i("Ticker",ticker);
        Log.i("Title",title);
        Log.i("Text",text);

        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);

        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }
}

When a notification arrives the log shows me the title, the package and the other informations correctly.. The problem is this one;

I have an Activity that is the MainActivity in which i have the NavigationDrawer. The class where i want show the incoming notifications is a Fragment! So i created in the MainActivity the Broadcastreceiver and then i inflate with LayoutInflater the layout of the fragment to get all component i need in this way:

public BroadcastReceiver onNotice= new BroadcastReceiver() {
        LinearLayout notificationLayout;
        Drawable icon;

        @Override
        public void onReceive(Context context, Intent intent) {
            final String pack = intent.getStringExtra("package");
            String title = intent.getStringExtra("title");
            String text = intent.getStringExtra("text");

            LayoutInflater mInf = LayoutInflater.from(context);
            View myView = mInf.inflate(R.layout.activity_main, null);
            notificationLayout = (LinearLayout)myView.findViewById(R.id.notificationLayout);
            TextView notificationDescription = (TextView) myView.findViewById(R.id.notificationDesc);
            TextView notificationTitle = (TextView) myView.findViewById(R.id.notificationTitle);
            CircularImageView notificationImage = (CircularImageView) myView.findViewById(R.id.img_thumbnail);

            Toast.makeText(DrawerActivity.this, title, Toast.LENGTH_SHORT).show();

            if(!pack.equals("") || !title.equals("") || !text.equals("")) {
                notificationLayout.setVisibility(View.VISIBLE);
                notificationTitle.setText(title);
                notificationDescription.setText(text);

                try {
                    icon = DrawerActivity.this.getPackageManager().getApplicationIcon(pack);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                notificationImage.setImageDrawable(icon);

            } else {
                notificationLayout.setVisibility(View.INVISIBLE);
            }

        }
    };

When the notification arrives now appears the Toast that notify me the title of the notification but i can't see the values in the view.. the layout of the notification is empty.. How is possible?

Or how can i put the values from the activity to my fragment?

Whne you inflate a layout, it creates a new tree made up of new objects.

The way to accomplish what you are trying to would be to cache whatever views that you will change in your onCreate and refer to them in your BroadcastReceiver's onReceive .

private TextView textView;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO: Update Views
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.my_fragment, container, false);

    //Cache views
    textView = (TextView) root.findViewById(R.id.textview);

    return root;
}

@Override
public void onResume() {
    super.onResume();

    getActivity().registerReceiver(broadcastReceiver, filter);
}

@Override
public void onPause() {
    super.onPause();

    getActivity().unregisterReceiver(broadcastReceiver);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM