0

アラームがトリガーされたときに通知を表示するアプリケーションに取り組んでいます。通知はアプリケーションがまだ実行中の場合にのみ表示されますが、アプリケーションが閉じられても通知が残るようにしたいので、ユーザーが通知を選択すると、アプリケーションが再度実行されます。それを行う方法はありますか?どんな助けでも大歓迎です。提供された例やチュートリアルにも感謝します。どうもありがとうございます。

4

2 に答える 2

3

私が使っているのはどうですか

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

今後、alarmMenagerで通知をプッシュしたいアプリがまだ実行されている間は機能しますが、alarm menagerを呼び出して通知を設定し、通知が表示されなかったアプリを閉じます。私がしなければならないこと ?

ここで、イベント送信者を取得しました:

Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR, HOUR_OF_DAY);
 cal.set(Calendar.MINUTE, MINUTE);
 //cal.add(Calendar.SECOND, SECOND_OF_DAY);
 Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class);
 intent.putExtra("alarm_status", statusMessage);
 intent.putExtra("alarm_title", title);
 intent.putExtra("alarm_content", content);
 Log.i("SenderEvent ", "przygotowane dane");
 PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

そしてレシーバー:

Bundle bundle = intent.getExtras();
        String statusMessage = bundle.getString("alarm_status");
        String title = bundle.getString("alarm_title");
        String content = bundle.getString("alarm_content");
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(), 0);
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());;
        //notif.largeIcon = bitmap;
        notif.icon =2130837504;
        notif.tickerText=statusMessage;
        notif.when= System.currentTimeMillis(); 
        /*
        new Notification(0,
                statusMessage, System.currentTimeMillis());*/
        notif.setLatestEventInfo(context, title, content, contentIntent);
        nm.notify(NOTIFY_ME_ID, notif);

アプリが閉じているときに通知をプッシュするのは何が問題なのですか?

于 2013-06-10T11:25:03.910 に答える
0

まず、サービスを使用して通知を作成する必要があります。以前も同じ質問がありました。phonegap を使用し、プラグインから通知を作成していましたが、バックグラウンドで実行できるのはサービスのみであることがわかりました。次に、サービスにコードを追加して通知を作成し、目的のためにブロードキャスト レシーバーを使用します。

      notificationManager = (NotificationManager) context.getSystemService(android.content.ContextWrapper.NOTIFICATION_SERVICE);
      note = new Notification(imageResource, tickerText, System.currentTimeMillis() );

     // Intent notificationIntent = new Intent(context, path.to.class);
      Intent notificationIntent = new Intent(context, package.path.to.receiver.class);

      notificationIntent.setAction(Intent.ACTION_VIEW);
      notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

      //contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      note.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

上記のコードでわかるように、getActivity をコメントアウトし、getBroadcast に変更しました。これはサービスで実行されているため、アプリを閉じている間に通知を受け取ることができます。閉じた場合にアプリを開くことができるようにするレシーバーのインテントについては、追加を追加します

...

@Override
public final void onReceive(Context context, Intent intent) {
    Log.v('TAG','TEST');


     //start activity
    Intent i = new Intent();
    i.setClassName("package.path", "package.path.mainActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
...

そしてxml

 <receiver android:name="path.to.receiver" android:label="@string/app_name">
   <intent-filter>
      <action android:name="path.to.receiver.BROADCAST" />
   </intent-filter>
</receiver>

これが役立つことを願っています

于 2012-09-04T05:09:43.670 に答える