5

Tôi có mã, và nó không hoạt động, vì vậy tôi yêu cầu bất cứ ai giúp tôi. Có rất ít thông tin về vấn đề cụ thể này.Tại sao Android bỏ qua việc tạo Thông báo?

MainActivity


public class MainActivity extends Activity { 
public final int PENDING_INTENT_ID = 8; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button clickity = (Button)findViewById(R.id.alarm_button); 
    clickity.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar now = Calendar.getInstance(); 
      now.add(Calendar.SECOND, 20); 

      //Create a new PendingIntent used by the Alarm when it activates 
      Intent intent = new Intent(v.getContext(), AlarmReciever.class); 
      intent.setAction("SOME_AWESOME_TRIGGER_WORD"); 
      intent.putExtra("info", "This String shows that the info is actually sent correctly"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(v.getContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT|Intent.FILL_IN_DATA); 

      //Then Create the alarm manager to send this pending intent and set the date to go off 
      AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent); 

     } 
    }); 

} 

AlarmReciever (Aware tôi đánh vần nó sai nhưng vì điều đó thế nào, im đi với nó)


public class AlarmReciever extends BroadcastReceiver{ 

@Override 
public void onReceive(Context c, Intent arg1) { 

    //get a reference to NotificationManager 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns); 

    //Instantiate the notification 

    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 
    Notification.Builder builder = new Notification.Builder(c) 
           .setTicker(tickerText) 
           .setWhen(when) 
           .setContentTitle(arg1.getStringExtra("info")) 
           .setContentText("Success!!") 
           .setAutoCancel(true); 
    Notification notification = builder.getNotification(); 
    mNotificationManager.notify(0, notification);//note the first argument, the ID should be unique 



} 
} 

Manifest


<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name="com.testproject.AlarmReciever" 
     android:enabled="true" 
     android:exported="false" > 
     <intent-filter> 
    </receiver> 

</application> 


Đó nên là tất cả mọi thứ. Tôi đang cố gắng để chạy nó như là và nó không hiển thị cho tôi bất cứ điều gì. Tôi đang chạy nó trên một trình mô phỏng thực sự là vấn đề.

EDIT: Khi tôi nói nó không hoạt động, tôi có nghĩa là không có gì bật lên. Nó chạy tốt, nhưng thông báo không bao giờ bật lên.

Vấn đề: enter image description here

Vì vậy, vấn đề này được thu hẹp xuống còn Android chỉ phớt lờ thông báo của tôi. Vấn đề là nó không cho tôi biết tại sao-vì vậy tôi không thể sửa chữa nó. Bất kỳ chuyên gia nào về vấn đề này có thấy điều gì đó sai với mã của tôi để gọi thông báo không? Liệu nó có quan trọng là nó trên một trình giả lập không?

Trả lời

2
Intent intent = new Intent(v.getContext(), AlarmReciever.class); 
// intent.setAction("SOME_AWESOME_TRIGGER_WORD"); replace to: 
intent.setAction("com.testproject.SOME_AWESOME_TRIGGER_WORD"); 

Nó ít nhất là cho cái nhìn đầu tiên

UPD:

long firstTime = SystemClock.elapsedRealtime(); 

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, /* INTERVAL IN MS */ 20 * 1000, /* PendingIntent */ intent); 
+0

Nếu tôi gỡ bỏ điều đó, tôi nên thay đổi tệp kê khai của mình là ' 'bằng cách nào? – Andy

+0

Bạn thực sự có thể xóa 'intent.setAction (..)' khỏi mã + loại bỏ phần '' ở tất cả từ tệp kê khai android. –

+0

Tôi vừa làm vậy. Vẫn không hoạt động. Cảm ơn bạn đã chỉ ra. Đó là một sự khởi đầu. – Andy

8

Vâng, bài học kinh nghiệm trên Notifications. Lý do tôi nhận được lỗi là vì một img PHẢI được thêm vào, nếu không, nó sẽ không hiển thị! Mọi thứ khác tôi đã có về cơ bản là đúng với ngoại lệ những gì Vladimir đã ân cần có thể chỉ ra. Đăng bài này ở đây trong trường hợp những người khác đang nhận được một vấn đề tương tự chỉ cần kiểm tra các thông báo.

22

Tôi đã gặp sự cố tương tự. Nếu bạn không chỉ định biểu tượng khi tạo "Thông báo mới()", thông báo sẽ không xuất hiện.

+1

Haha, yea. Tôi muốn các tài liệu đề cập đến điều đó. Nhưng hey, thử và sai phải. Bây giờ những người khác biết :) – Andy

0

Điều này là do, bạn không đặt biểu tượng thông báo. Nếu bạn đặt biểu tượng notificaiton thì nó sẽ hoạt động. Bản thân nhật ký nói rằng bạn không thể gửi thông báo mà không có hình ảnh.

0

Có vẻ như bạn quên đặt Biểu tượng .. Bạn cần đặt ít nhất một biểu tượng mặc định ..

Hãy thử điều này ..

Notification.Builder builder = new Notification.Builder(c) 
         .setTicker(tickerText) 
         .setWhen(when) 
         .setContentTitle(arg1.getStringExtra("info")) 
         .setContentText("Success!!") 
         .setAutoCancel(true) 
         .setSmallIcon(R.drawable.ic_launcher); //<--- this one