我有以下代码:
public void sendNotification() {
try {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
final Intent intent = new Intent(this, NotifyBroadcastReceiver.class);
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo));
builder.setContentTitle("Notifications Title");
builder.setContentText("Your notification content here.");
builder.setSubText("Tap to view the website.");
builder.setAutoCancel(true);
final Intent noted = new Intent(this, NotifyBroadcastReceiver.class);
noted.setAction("com.mawaeed.common.LaunchActivity");
PendingIntent notedpendingIntent = PendingIntent.getBroadcast(this, 0, noted, 0);// PendingIntent.FLAG_UPDATE_CURRENT ) ;
builder.addAction(0, "Noted", notedpendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}catch(Exception exo) {
Toast.makeText(this, exo.toString(),Toast.LENGTH_LONG).show();
}
}
还有我的BroadcastReceiver
public class NotifyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"ddd",Toast.LENGTH_LONG).show();
Toast.makeText(context,"app",Toast.LENGTH_LONG).show();
}}
我正在从FirebaseMessagingService调用sendNotification,通知会正常显示。
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification();
}
[单击通知或注释操作时,BroadcastReceiver onReceive未呼叫,我已经在mainafist中注册了BroadcastReceiver
<receiver android:name=".NotifyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
奇怪的是,我创建了一个新应用程序并将其上面的所有代码复制到该应用程序,然后从onCreate()调用sendNotification,并且在单击通知时它会毫无问题地调用onReceive。
我也对我的应用程序进行了相同的尝试,并从onCreate的主要活动中调用了sendNotification,出现了Notification,但单击通知或Noted操作未调用onReceive,则>
为什么无法在我的应用程序中正常工作
我有以下代码:public void sendNotification(){试试{NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(android.R ....
我必须先卸载该应用程序,然后再重新安装,现在它可以工作。