【问题标题】:How to create a notification when my app receive a broadcast message当我的应用收到广播消息时如何创建通知
【发布时间】:2019-10-09 21:26:40
【问题描述】:

我的应用只是接收来自另一个应用的广播。我是广播接收器的新手,所以我遇到了问题。 我希望当我的应用收到广播消息时,通知面板中会出现通知。 我尝试了很多,但在互联网上一无所获。

我尝试了 youtube 视频和 StackOverflow,但一无所获。

公共类 MyReceiver 扩展 BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "EBR triggered", Toast.LENGTH_LONG).show();

// 下面的代码不起作用。我从 StackOverflow 复制这个 // PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0);

    Notification.Builder builder = new Notification.Builder(context);

    builder.setAutoCancel(false);
    builder.setTicker("Ticker text");
    builder.setContentTitle("Content of Notification");
    builder.setContentText("You have a new message");
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(true);
    builder.setSubText("This is subtext...");  
    builder.setNumber(100);
    builder.build();

    // add as notification
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());


}


}

没有错误,我预计广播接收时会出现一个通知图标,但什么也没发生。

【问题讨论】:

  • 我真的不知道我在说什么,但我还是会发布它,因为你还没有得到任何答案。 ;-) 1:您是否在清单中声明了广播接收器? 2:您是否创建了通知渠道?此外,您不应调用 builder.build() 两次。
  • 这两件事我都做了。顺便说一句,我会错误地在这里粘贴两次构建器方法
  • 谢谢!我删除了该行并知道它有效:)

标签: android broadcastreceiver android-notifications


【解决方案1】:

这是一个简单的广播接收示例。

应用 1(发件人)

Intent intent = new Intent("MY_NOTIFICATION");
intent.setComponent(
    new ComponentName("com.example.stackoverflow", "com.example.stackoverflow.MyReceiver")
);
intent.putExtra("data","Notice me senpai!");
sendBroadcast(intent);

应用 2(接收方)

AndroidManifest.xml:

...
<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="MY_NOTIFICATION"/>
    </intent-filter>
</receiver>

MyReceiver.java:

public void onReceive(Context context, Intent intent) {
        String data = intent.getStringExtra("data");
        Toast.makeText(context, data, Toast.LENGTH_LONG).show();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("description");
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("textTitle")
                .setContentText(data)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        notificationManager.notify(1, builder.build());
    }

请注意,自 Android 8 以来就有 Broacast limitations。因此您需要提供显式类来处理,即 setComponent 参数以及操作

【讨论】:

  • 由于我是这个领域的新手,请告诉我如何明确处理 setComponent 参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多