我有以下代码:
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 not calling,我已经在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,此时会出现通知,但单击通知或注意到的操作没有调用onReceive
为什么它在我的应用程序中不起作用
发布于 2020-04-26 07:02:51
我必须先卸载这个应用程序,然后再安装它,现在它可以工作了。
https://stackoverflow.com/questions/61419091
复制相似问题