Androidアプリで「動作しました」という通知を設定しようとしていますが、API 1に至るまで互換性を持たせる必要がありますが、これを行う方法については本当に混乱しています。非推奨の古いチュートリアルと、古いAPIレベルをサポートしない新しいチュートリアルがあります。このSOの質問によると、 NotificationCompat.Builderを使用する必要があります。私が使用している例がありますが、コードを完全には理解していません。
このコードから:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
下に赤い線が表示されます:ctx
、、、YOUR_PI_REQ_CODE
YOUR_NOTIF_ID