广播接收器中未显示通知



即使应用程序打开或关闭,通知也不会到来。但只有当应用程序打开时,祝酒词才会出现。我有一个主要活动和另一个扩展broadcastlistener的类。在活动中,我请求权限,在课堂上,我编写了来电通知代码。请帮我处理一些代码片段。下面是我的活动课。

public class PhoneActivity extends AppCompatActivity {
private static final int REQUEST_READ_PHONE_STATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
int readContactsPermissionLog = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CALL_LOG);
if(readContactsPermissionLog != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, REQUEST_READ_PHONE_STATE);
}
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
}
}
}

这是我的扩展广播接收机的类。

public class PhoneState extends BroadcastReceiver {
public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
boolean connected  = true;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state != null) {
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (!intent.getExtras().containsKey(TelephonyManager.EXTRA_INCOMING_NUMBER)) {
Log.i("Call receiver", "skipping intent=" + intent + ", extras=" + intent.getExtras() + " - no number was supplied");
return;
}
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("incoming number is " + number);
if (number != null) {
if (number.equals("some number i have given for testing")) {
System.out.println("number matched");
createNotificationChannel(number,context);
createNotificationChannel(number,context);
// Toast.makeText(context, "number is " + number, Toast.LENGTH_SHORT).show();
} else {
createNotificationChannel(number,context);
//  Toast.makeText(context, "number is " + number, Toast.LENGTH_LONG).show();
System.out.println("not matched");
}
} else {
System.out.println("number is null");
}
}
}
}
}


private void createNotificationChannel(String number,Context context) {
String CHANNEL_ID = "10";
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Phone";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(number);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

我已将其添加到我的清单文件中。

<receiver android:name=".PhoneState"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

这是我的布局活动。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.PhoneActivity">
</LinearLayout>

可能是您没有在Manifist中添加服务。如果活动由安卓工作室自动添加,但安卓不会在Manifist中自动添加服务。所以你需要在你的疯狂拳中添加这个代码。

<service
android:name=".FireBaseMessagingService.GettingDeviceTokenService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".FireBaseMessagingService.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

我希望这对你有帮助。

请确保您的android版本低于8,否则您应该使用Notification Channel创建通知https://developer.android.com/training/notify-user/channels