首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不从NotificationManager触发BroadcastReceiver

不从NotificationManager触发BroadcastReceiver
EN

Stack Overflow用户
提问于 2020-04-25 07:41:38
回答 1查看 70关注 0票数 0

我有以下代码:

代码语言:javascript
复制
 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

代码语言:javascript
复制
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,通知正常出现。

代码语言:javascript
复制
   public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification();
}

当单击通知或注意到的操作时,BroadcastReceiver onReceive not calling,我已经在mainafist中注册了我的BroadcastReceiver

代码语言:javascript
复制
    <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

为什么它在我的应用程序中不起作用

EN

回答 1

Stack Overflow用户

发布于 2020-04-26 07:02:51

我必须先卸载这个应用程序,然后再安装它,现在它可以工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61419091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档