0
SMS受信で通知するカスタムソリューションを作成しようとしています。 私はStackoverflowのいくつかの解決策を見つけましたが、うまくいきません。以下は私のコードです。Android SMS通知生成
public class MainActivity extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context,"Message Received", Toast.LENGTH_LONG);
toast.show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("SMS Received");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
とマニフェストファイル:
<receiver android:name=".MainActivity"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>