我的应用程序,执行对远程数据库的调用,因此定期进行调用以读取json响应,并且如果它是特定值,则应发送通知。它可以使他的工作正常,但是会打开发送通知的活动。我应该意识到的是,在控件内部,该应用程序会发出通知,并且仅当用户单击它时才打开带有完整消息的活动,这可能吗?
这是我的代码

public class AlarmReceiver extends BroadcastReceiver {
SharedPreferences sharedPreferences;
String userid, datareq;

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences prefs = context.getSharedPreferences("My_App",
            Context.MODE_PRIVATE);
    String userid = prefs.getString("userid", "");
    String datareq = prefs.getString("datareq", "");

    String[] params = new String[2];
    String[] result = new String[2];
    params[0] = userid;
    params[1] = datareq;


    try {
        result = new TaskControl(context.getApplicationContext()).execute(params).get();
        if (result[1].equals("-1")) {
            //failed

        } else {

   // Here i call NotificationView.class, but i should want send
   // directly the notify
            Intent msg = new Intent(context, NotificationView.class);
            msg.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(msg);

        }

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}


这是使用您的帮助进行的EDITED课程

   public class AlarmReceiver extends BroadcastReceiver {
SharedPreferences sharedPreferences;
String userid, datareq;
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences prefs = context.getSharedPreferences("Beauty_App",
            Context.MODE_PRIVATE);
    String userid = prefs.getString("userid", "");
    String datareq = prefs.getString("datareq", "");

    String[] params = new String[2];
    String[] result = new String[2];
    params[0] = userid;
    params[1] = datareq;


    try {
        result = new TaskControl(context.getApplicationContext()).execute(params).get();
        if (result[1].equals("-1")) {
            //failed
            Toast.makeText(context, "failed", Toast.LENGTH_LONG).show();

        } else {
            // if i use notify, app stops

         Notify ("Msg","Title");


        }

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

    private void Notify(String notificationTitle, String notificationMessage) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        @SuppressWarnings("deprecation")

        Notification notification = new Notification(R.drawable.scanner,"New Message", System.currentTimeMillis());
        Intent notificationIntent = new Intent(context,NotificationView.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

        notification.setLatestEventInfo(context, notificationTitle, notificationMessage, pendingIntent);
        notificationManager.notify(9999, notification);
    }


}

最佳答案

将代码从NotificationView.Notify()方法移动到开始NotificationView活动的else部分。

并仅在用户单击通知时打开此活动,才能创建如下待处理的意图:

Intent intent = new Intent(this, NotificationView.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


然后使用NotificationCompat.Builder设置此意图,如下所示:

Notification notification = new NotificationCompat.Builder(ctx).setContentIntent(pendingNotificationIntent).build();

关于android - 在BroadcastReceiver内部通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33957137/

10-11 22:21
查看更多