应用被杀死时,Android推送通知不起作用

时间:2018-07-08 19:18:18

标签: java android push-notification notify

我尝试在我的Android应用中发出通知。但是只要不终止应用程序,它就可以正常工作。诀窍是什么?有人可以帮我吗? 我的代码:广播来自服务。

private NotificationCompat.Builder notificationBuilder;
private NotificationManagerCompat notificationManager;


private void createNotificationChannel(){

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.CHANEL_NAME);
                String description = getString(R.string.CHANEL_DESCRIPTION);
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);

                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
        }

private void receiveBrodcasts(){
            notificationFirebaseFriendsBrodcastReceiver.setFirebaseFriendsBrodcastReceiver(new NotificationFirebaseFriendsBrodcastReceiver.FirebaseFriendsBrodcastReceiver() {
                @Override
                public void onBrodcastReceive(Context context, Intent intent) {

                    String key = intent.getStringExtra("KEY");
                    String user_nickname = intent.getStringExtra("USER_NICKNAME");
                    String pkey = intent.getStringExtra("PKEY");
                    int event = intent.getIntExtra("EVENT", -1);

                    onFriendsStatusChanged(key, event, user_nickname, pkey);


                    Intent alertIntent = new Intent(activity, FriendsProfileActivity.class);
                    alertIntent.putExtra("FRIENDS_ID", key);
                    alertIntent.putExtra("FRIEND_STATUS", event);
                    alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, alertIntent, 0);

                    notificationBuilder = new NotificationCompat.Builder(activity, CHANNEL_ID)
                            .setSmallIcon(R.drawable.ic_logo)
                            .setContentTitle(user_nickname)
                            .setContentText(getNotificationString(event))
                            .setContentIntent(pendingIntent)
                            .setAutoCancel(true)
                            .setPriority(NotificationCompat.PRIORITY_HIGH);

                    notificationManager.notify(2, notificationBuilder.build());
                }
            });
        }

非常感谢您的帮助。

0 个答案:

没有答案